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的测试操作符

OperatorTrue If...
-b filefile exists and is a block device file
-c filefile exists and is a character device file
-d filefile exists and is a directory
-e filefile exists
-f filefile exists and is a regular file
-g filefile exists and has its setgid bit set
-G filefile exists and is owned by the effective group ID
-k filefile exists and has its sticky bit set
-L filefile exists and is a symbolic link
-n stringstring is non-null
-O filefile exists and is owned by the effective user ID
-p filefile exists and is a pipe or named pipe (FIFO file)
-r filefile exists and is readable
-s filefile exists and is not empty
-S filefile exists and is a socket
-t NFile descriptor N points to a terminal
-u filefile exists and has its setuid bit set
-w filefile exists and is writeable
-x filefile exists and is executable, or file is a directory that can be searched
-z stringstring has a length of zero
fileA -nt fileBfileA is newer than fileB
fileA -ot fileBfileA is older than fileB
fileA -ef fileBfileA and fileB point to the same file
stringA = stringBstringA equals stringB
stringA != stringBstringA does not match stringB
stringA < stringBstringA sorts before stringB lexicographically
stringA > stringBstringA sorts after stringB lexicographically
exprA -eq exprBArithmetic expressions exprA and exprB are equal
exprA -ne exprBArithmetic expressions exprA and exprB are not equal
exprA -lt exprBexprA is less than exprB
exprA -gt exprBexprA is greater than exprB
exprA -le exprBexprA is less than or equal to exprB
exprA -ge exprBexprA is greater than or equal to exprB
exprA -a exprBexprA is true and exprB is true
exprA -o exprBexprA 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可以处理特殊的文件,包含有空格...

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值