Bash使用

Bash使用

重要

用bashdb来调试bash的脚本, 可以在emacs中用命令bashdb直接调试, 我的.emacs已经做了初始化工作了

bash的indirect reference-- Assume that the value of a variable is the name of a second variable.

	old bash:
		eval a=/$eth$i
	>2.o 
		${!a}
		

 

用seq可以得到数字的范围,如1-100

从tar.gz文件名称中得到展开的dir的名称

REALDIRNAME=`tar tfz $1 | head -1 | sed -e 's/^/.; s///$//;'`
		

 

&&可以保证前面一条命令成功后,在执行下一条命令,如

./configure &&
make &&
make install
		

 

可以用DEBUG trap来调试

dbgtrap()
{
    echo "badvar is badvar"
}

trap dbgtrap DEBUG
echo "aaaaaaaaa"
echo "bbbbbbbbb"
trap - DEBUG    # turn off the DEBUG trap
		

 

读取stdin

 

read command ip protocol port upband downband maxtcpconnect havenat natip
echo $command
echo $ip
echo $protocol 
echo $port
echo $upband 
echo $downband 
echo $maxtcpconnect
echo $havenat
echo $natip        

        

 

Basename,dirname

 

        

 

字符串截取

 

            $ MYVAR=foodforthought.jpg2005-9-15
            $ echo ${MYVAR##*fo}	#---表示前面,因为在键盘上#在$的前面
            rthought.jpg
            $ echo ${MYVAR#*fo}
            odforthought.jpg
            
            $ MYFOO="chickensoup.tar.gz"
            $ echo ${MYFOO%%.*} 	%---表示后面,因为在键盘上%在$的后面
            chickensoup
            $ echo ${MYFOO%.*}
            chickensoup.tar
            $ EXCLAIM=cowabunga
            $ echo ${EXCLAIM:0:3}
            cow
            $ echo ${EXCLAIM:3:7}
            abunga
        
        

 

输入参数

 

             #!/usr/bin/env bash
             
             echo name of script is $0
             echo first argument is $1
             echo second argument is $2
             echo seventeenth argument is $17
             echo number of arguments is $#
        
        

 

if statement

 

            if [ condition ] ; then 
                action
            elif [ condition2 ] ; then 
                action2
            .
            .
            .
            elif [ condition3 ] ; then 
            
            else
                actionx
            fi
            

表 11.3. bash的测试操作符

Operator True If...
-b file file exists and is a block device file
-c file file exists and is a character device file
-d file file exists and is a directory
-e file file exists
-f file file exists and is a regular file
-g file file exists and has its setgid bit set
-G file file exists and is owned by the effective group ID
-k file file exists and has its sticky bit set
-L file file exists and is a symbolic link
-n string string is non-null
-O file file exists and is owned by the effective user ID
-p file file exists and is a pipe or named pipe (FIFO file)
-r file file exists and is readable
-s file file exists and is not empty
-S file file exists and is a socket
-t N File descriptor N points to a terminal
-u file file exists and has its setuid bit set
-w file file exists and is writeable
-x file file exists and is executable, or file is a directory that can be searched
-z string string has a length of zero
fileA -nt fileB fileA is newer than fileB
fileA -ot fileB fileA is older than fileB
fileA -ef fileB fileA and fileB point to the same file
stringA = stringB stringA equals stringB
stringA != stringB stringA does not match stringB
stringA < stringB stringA sorts before stringB lexicographically
stringA > stringB stringA sorts after stringB lexicographically
exprA -eq exprB Arithmetic expressions exprA and exprB are equal
exprA -ne exprB Arithmetic expressions exprA and exprB are not equal
exprA -lt exprB exprA is less than exprB
exprA -gt exprB exprA is greater than exprB
exprA -le exprB exprA is less than or equal to exprB
exprA -ge exprB exprA is greater than or equal to exprB
exprA -a exprB exprA is true and exprB is true
exprA -o exprB exprA is true or exprB is true

 

For statement

 

            for myfile in /etc/r*
             do
                 if [ -d "$myfile" ] 
            then 
                   echo "$myfile (dir)"
            else
                   echo "$myfile"
            fi
             done
        
        

 

算术计算

 

            $ echo $(( 100 / 3 ))
             33
             $ myvar="56"
             $ echo $(( $myvar + 12 ))
             68
             $ echo $(( $myvar - $myvar ))
            0 $ myvar=$(( $myvar + 1 ))
            $ echo $myvar
             57
        
        

 

while until statement

 

            while [ condition ]
             do
                 statements
             done
            
            myvar=0
             until [ $myvar -eq 10 ]
             do
                 echo $myvar
                 myvar=$(( $myvar + 1 ))
             done
        
        

 

case statement

 

            case "${x##*.}" in
                  gz)
                        gzunpack ${SROOT}/${x}
                        ;;
                  bz2)
                        bz2unpack ${SROOT}/${x}
                        ;;
                  *)
                        echo "Archive format not recognized."
                        exit
                        ;;
             esac          

        

 

函数 命名空间

 

            #!/usr/bin/env bash
             
             myvar="hello"
             
             myfunc() {
                 local x
                 local myvar="one two three"
                 for x in $myvar
                 do
                     echo $x
                 done
             }
             
             myfunc
             
             echo $myvar $x
        

 

时间的取得和修改

修改系统时间用date,修改硬件时间用clock或者hwclock

					date +%Y%m%d --显示20030906
					date +%s     --显示秒
					date --date='yesterday' +%Y-%m-%d  --显示昨天的日期
					date 04210902200305 #修改系统时间
					hwclock --systohc   #把系统时间同步到硬件时间
					#Pad with zeroes, spaces (%_), or nothing (%-)
		        

 

取得一些文件名称中的一部分名称

 

interfaces=`ls session_* | sed 's/^session_//g' | sed 's//.log$//g'`
		        

 

如何产生随机数

例 11.4. 如何产生随机数

 

					dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -f1 -d" " 
		        

 

如何用bash找到一个目录下的所有的子目录

例 11.5. 如何用bash找到一个目录下的所有的子目录

 

dirs=`find . -type d`
for dir in $dirs
do
	echo $dir
done
		        

 

如何用bash从文件中读一行,显示

例 11.6. 如何用bash从文件中读一行,显示

 

while read line
  do
  echo $line
done < "$filename"
		        

 

如何用bash从发现某些文件中是否没有包含string

例 11.7. 如何用bash发现某些文件中是否没有包含string

 

jsps=`find . -name "*.jsp"`

for jsp in $jsps; do
#       echo $jsp
    havegb=`grep gb2312 $jsp`
        [ ! -n "$havegb" ] && echo $jsp
done

		        

 

tips

xargs可以处理特殊的文件,包含有空格...

 

你可能感兴趣的:(Bash使用)