Special Characters

  • 注释
# this is a line comment
echo "A comment will follow" # comment here

;

  • command seprator
echo hello; echo there

if [ -x "$filename" ]; then
echo "$filename is exists"; cp $filename $filename.bak
else
echo "$filename is not found"; touch $filename
fi; echo "file test complete"

;;

  • teminator in case option
case $variable in
abc) echo "\$variable = abc" ;;
xyz) echo "\$variable = xyz" ;;
esac

,

  • comma operator
let "x = ((a = 9, 15 / 3))"
# set a = 9 and x = 15 / 3

for file in /{,usr/}bin/*calc
# find all files execuable ending in calc
#+ in /bin and /usr/bin directories
do
  if [ -x "$file" ]
    echo $file
  fi
done

:

  • null command
  • 某些用法有点类似 其他语言里面的 占位符
while :
do
...
end

# same as
# while true
# do
# ...
# end

if condition
then :
# do some action
fi

: ${usernane=`whoami`}

# ${username=`whoami`}
# Gives an error without the leading :
# unless username is an command or builtin

?

  • test operator
  • 作为三目运算符用
((var0 = var1<98?9:1))

$?

  • exit status variable

$$

  • process ID variable

()

  • command group
  • 局部作用于
  • array initialization
(a=hello; echo $a)
a=123
( a=321; )
echo "a = $a"
# outout a = 123
# a within a parentheses acts like a local variable

Array=(element1, element2, element3)

{xxx,yyy,zzz}

  • brace expansion
echo \"{these,words,are,quoted}\"
# "these" "words" "are" "quoted"

cat {file1,file2,file3} > combined_file
# Concatenates the files file1, file2, and file3 into combined_file

cp file22.{txt,bak}
# Copies file22.txt to file22.bak

echo {file1,file2}\ {\ A," B",' C'}
# file1 : A file1 : B file1 : C file2 : A file2 : B file2 : C 
# No spaces are allowed unless the spaces are quoted or escaped

echo {a..z}
# Echoes characters between a and z

echo {1..3}
# Echoes characters between 1 and 3

base64_charset=( {A..Z} {a..z} {0..9} + / = )
#

{}

  • block of code
  • this construct , in effect , creates an anonymous function (function without a name), However unlike a "standand" function , The variables in a code block remain visible to the remainder of the script
a = 123
{a = 321;}
echo "a = $a" # a = 321
  • Code blocks and I/O redirection
#!/bin/bash
#reading lines in /etc/fstab

File=/etc/fstab

{
read line1
read line2
} < $File

echo "the first line in $File is"
echo $line1
echo
echo "The second line in $File is"
echo line2

exit0
  • Saving the output of a code bloack to a file
#!/bin/bash
#rpm_check.sh

SUCESS=0
E_NOARGS=65

if [ -z "$1" ]; then
echo "Usage: `basename $0` rpm file"
exit E_NOARGS
fi

{
echo
echo "Archive description"
rpm -qpi $1
echo
echo "Archive Listing"
rpm -qpl $1
echo
rpm -i test
if [ "$?" -eq "$SUCESS" ]; then
echo "$1 can be installed"
else
echo "$1 can not be installed"
fi
echo
} > "$1.test"

echo "Results of rpm file in $1.test"

exit 0

> &> >& >> < <>

  • redirection
scriptname >filename
# Redirects the output of scriptname to file filename.Overwrite filename if it already exists.

command &>filename 
# Redirects both the stdout and the stderr of command to filename

command >&2
# Redirects stdout of the command to stderr

scriptname >>filename
# Append the output of scrptname to filename.
If filename does not exist already, it is created.

[i] <>filename 
# Opens file filename for reading and writing,
and asigns file descriptor i to it. If filename does not exist, create it.

|

  • pipe
echo ls -l | sh
cat *.lst | sort | uniq
cat $filename1 $fielname2 | grep $search_word

&

  • run job in background
sleep 10 &

-

  • option,prefix
if [ $file1 -ot $file2 ];then
echo "File $file1 is older $file2"
fi

if [ "$a" -eq "$b" ]; then
echo "$a is equal to $b"
fi

if [ "$c" -eq 24 -a "$d" -q 47 ]; then
echo "$c equals to 24 and $d equals to 47"
end

params2=${$param1:-DEFAULTVALUE}

命令

ctrl w # 删除一个单词