shell programming knowledge

shell programming
shell is a scripting language
you can use logic, grammar circulation
can customize the function of
the collection system command shell is a
shell script can be automated operation and maintenance, can greatly increase the efficiency of our operation and maintenance

Beginning need to add # / bin / bash!
To the line beginning with # as the explanation
name of the script with .sh end, which is used to distinguish a shell script
execution in two ways
chmod + x 1.sh; ./1.sh
bash 1.sh
view the script execution bash -x 1.sh
to see whether the script syntax errors bash -n 1.sh

date Time command
date +% Y-% m-% d = date +% F date
date +% y-% m-% d date
date +% H:% M: % S = date +% T time
date +% s timestamp
DATE -d @ 1504620492
DATE -d "+ 1day" one day after the
date -d "-1 day" the day before the
date -d "-1 month" before January
date -d "-1 min" a minutes before
date +% w weekday
date +% W week number of year

Variable shell script
when the script is a character string used more frequently, and should be used instead of a string variable long length
using a conditional statement, is often used variables if [$ a -gt 1]; then ...; fi
when referring to the results of a command to replace the variable with n = wc -l 1.txt
script writing and user interaction, the variable is also essential read -p "Input a number:" n; echo $ n If you did not write the n, can be used directly $ REPLY
built variables $ 0, $ 1, $ 2 ... $ 0 for the script itself, $ 1 the first parameter, $ 2 second .... $ # represents the number of parameters of
the mathematical operation a = 1; b = 2; c = $ (( $ a + $ b)) or $ [$ a + $ b]

Analyzing shell script logic

Format 1: if condition; then statements; fi a = 5 if [$ a -gt 3]; then echo ok; fi
Format 2: if condition; then statement; else statement; fi a = 5 if [$ a -gt 3 ]; then echo ok; else not ok; fi
format 3: if ...; then ...; elif ...; then ...; else ...; fi a = 5 if [$ a -gt 1]; then echo ok; elif [$ a -lt 6]; else nook; fi
logic determination expression:
IF [-gt $ A $ B];
IF [$ -LT-A. 5];
IF [$ B -eq 10] et
-gt (>); greater than
- lt (<); less than
-ge (> =); greater than or equal
-le (<=); less than or equal
-eq (==); equal
-ne not equal (! =)
Note everywhere spaces
may be used && || combining a plurality of condition
if [$ a -gt 5] && [$ a -lt 10]; then , and
if [$ b -gt 5] || [$ b -lt 3]; then or

Directory attribute determining
[-f file] determines whether the file is normal, and there = F "/ tmp / ceshi" IF [-f $ F]; the then echo $ F exist; Touch the else $ F; Fi
[-d File] determining whether the directory, and the presence of f = "/ tmp / ceshi" iF [-d $ F]; the then echo $ F exist; Touch the else $ F; Fi
[file -e] Analyzing the file or directory exists f = "/ tmp / ceshi "iF [-e $ F]; the then echo $ F exist; Touch the else $ F; Fi
[-R & lt file] determines whether the file is readable f = '' / tmp / ceshi '' if [-r $ f ]; $ F IS Read the then echo; Fi
[-w file] determines whether the file can be written = F '' / tmp / ceshi '' iF [-w $ F]; IS write the then echo $ F; Fi
[the -X-file ] determines whether an executable file f = '' / tmp / ceshi '' if [-x $ f]; then echo $ f is exe fi

[ -f $f ] && rm -rf $f f=''/tmp/ceshi'' if [ -f $f ] ; then rm -rf $f ; fi
[ ! -f $f ] || touch $f f=''/tmp/ceshi'' if [ ! -f $f ] ; then touch $f ;fi

determining if some special usage
if [-z "$ a"] This represents what happens when the variable value is a space-time
if [-n "$ a"] represents the value when the variable a is not empty
if grep -q '123' 1.txt; represents if the then 1.txt contained in '123' in row happen
if [-e file!]; then represents what happens when the file does not exist
if (($ a <1) ) ; equivalent to the then ... IF [$ -LT-a. 1]; the then ...
[] can not be used <,>, ==, =,> =, <= such a symbol!

the shell case is determined
format case variable names in
VALUE1)
Command
;;
value2)
Command
;;
*)
Command
;;
may be used in the case in a condition |, or represents a meaning, such as 2 |. 3) ;; Command
Read -p ' "

shell脚本案例
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi

= N1 echo $n|sed 's/[0-9]//g'// investigation where non-digital
IF [-n "$ N1"]
the then
echo "INPUT A Number Please."
Exit. 1
Fi

if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi
case $tag in
1)
echo "not ok"
;;
2)
echo "ok"
;;
3)
echo "ook"
;;
4)
echo "oook"
;;
*)
echo "The number range is 0-100."
;;
esac

The for loop
syntax for variable names in condition; do ............; done

Classic Case 1
#! / bin / the bash
SUM = 0
for I in seq 1 100
do
SUM = $ [$ $ SUM + I]
echo $ I
DONE

echo $sum

File list loop
#! / Bin / bash
cd / etc /
for A in ls /etc/
do
    IF [-d $ A]
    the then
       LS -d $ A
    fi
DONE

Guess you like

Origin blog.51cto.com/865516915/2430194