博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

linux shell学习

Posted on 2013-10-11 10:30  bw_0927  阅读(158)  评论(0)    收藏  举报

http://www.cnblogs.com/chengmo/tag/shell/

 grep不行的时候换egrep,或者grep -E 高级正则

grep -RinE 

grep -c 打印匹配的个数

管道,重定向,命令替换

 grep -B 3 'string' file     打印匹配行的前三行

 

grep -A 13 -Rh 'ngx_module_t' .   

-A 13打印匹配行和之后的13行

-h  不打印匹配的文件名

 

命令替换

命令替换是把命令执行后的标准输出放入变量中。


var=$(命令)
var=`命令`
取变量值的时候别加括号 v=$var

在这个过程中,默认会自动删除命令结果里的换行符。

2:使用 $(( ))
r=$(( 4 + 5 ))
echo $r

3:使用 $[ ]

r=$[ 4 + 5 ]
echo $r

 

http://www.freeos.com/guides/lsst/


clear    清屏
exit 0
echo -e "Today is \c "; date
echo -n `expr 10 \* 3`
echo ""     #Print the blank line
command1;command2   同一行多个命令间用冒号,case语句用双冒号,其他时候无需冒号
$# holds number of arguments specified on command line. And $* or $@ refer to all arguments passed to script.
test command or [ expr ]
if test 5 -eq 6或者  if [ 5 -eq 6 ]
if test string1 = string2
-n string1   #string1 is NOT NULL and does exist
-z string1     #string1 is NULL and does exist
-s file        #Non empty file
-f file        #Is File exist or normal file and not a directory
! expression    #Logical NOT
expression1  -a  expression2    #Logical AND
expression1  -o  expression2    #Logical OR

if condition
then
           condition is zero (true - 0)
           execute all commands up to elif statement
elif condition1
then
           condition1 is zero (true - 0)
           execute all commands up to elif statement  
elif condition2
then
           condition2 is zero (true - 0)
           execute all commands up to elif statement          
else
           None of the above condtion,condtion1,condtion2 are true (i.e.
           all of the above nonzero or false)
           execute all commands up to fi
fi


for i in 1 2 3 4 5
do
echo "Welcome $i times"
done


for (( expr1; expr2; expr3 ))
do
   .....
   ...
   repeat all statements between do and
   done until expr2 is TRUE
Done


for ((  i = 0 ;  i <= 5;  i++  ))    //空格可有可无
do
  echo "Welcome $i times"
done


while [ condition ]
do
     command1
     command2
     command3
     ..
     ....
done


while :   //死循环
do
done

case  $variable-name  in
    pattern1)   
                command
                    ...
                    ..
                command
                ;;
    pattern2)   
                command
                    ...
                    ..
                command
                ;;
    patternN)   
                command
                    ...
                    ..
                command
                ;;
    *)          
                command
                    ...
                    ..
                command
                ;;
esac

shell调试
sh -x dsh1.sh 4 5
Option can be:
-v Print shell input lines as they are read.
-x After expanding each simple-command, bash displays the expanded value of PS4 system variable, followed by the command and its expanded arguments.


/dev/null - Use to send unwanted output of program
To set global varible you have to use export command.
Syntax:
export variable1, variable2,.....variableN

Example:
$ rm myf && echo "File is removed successfully" || echo "File is not removed"
If file (myf) is removed successful (exist status is zero) then "echo File is removed successfully" statement is executed, otherwise "echo File is not removed" statement is executed (since exist status is non-zero)


sayHi()   //函数定义
{
    echo "hi"
}
sayHi;   //函数调用


shell信号捕捉:
Syntax:
trap {commands} {signal number list}

Signal Number    When occurs
0    shell exit
1    hangup
2    interrupt (CTRL+C)
3    quit
9    kill (cannot be caught)
example:
trap del_file 2   //收到信号2,ctrl+c时,执行del_file函数


The shift command moves the current values stored in the positional parameters (command line args) to the left one position. For example, if the values of the current positional parameters are:
$1 = -f $2 = foo $3 = bar
and you executed the shift command the resulting positional parameters would be as follows:
$1 = foo $2 = bar
You can also move the positional parameters over more than one place by specifying a number with the shift command.
shift 2


Syntax:
getopts {optsring} {variable1}
getopts is used by shell to parse command line argument.
"optstring contains the option letters to be recognized; if a letter is followed by a colon, the option is expected to have an argument, which should be separated from it by white space.
Each time it is invoked, getopts places the next option in the shell variable variable1, When an option requires an argument, getopts places that argument into the variable OPTARG.
On errors getopts diagnostic messages are printed when illegal options or missing option arguments are encountered. If an illegal option is seen, getopts places ? into variable1."

字符串拼接1:
[root@localhost sh]# var1=/etc/
[root@localhost sh]# var2=yum.repos.d/
[root@localhost sh]# var3=${var1}${var2}
字符串拼接2:
$value1=home
$value2=${value1}"="
echo $value2


newMD5Val=`md5sum $tmpUpdateDir$fileName`
echo $newMD5Val
curMD5Val=`md5sum ./$fileName`
echo $curMD5Val     #直接取变量,可不加双引号
if  [ "$newMD5Val" != "$curMD5Val" ]; then   #条件判断时,必须得加双引号
    echo "exit
fi

 

http://www.cnblogs.com/haoshikui/archive/2011/12/29/2306434.html

批量kill进程

ps -ef|grep aaa|grep -v grep|awk  '{print "kill -9 " $2}' | sh


Shell 脚本中有个变量叫 IFS(Internal Field Seprator) ,内部域分隔符。

#method 4
#  $(cmd) equals `cmd`
for line in $(cat /home/serverList)
do
    echo $line
done
echo

#method 5
IFS_old=$IFS
IFS=$'\n'
for line in `cat /home/serverList`
do
    echo $line
done
IFS=$IFS_old




let和双括号取变量都不需要$号

num=0
while ((num < wordsNu))
do
let num+=1
echo $num
done

 

  • command1  && command2
&&左边的命令(命令1)返回真(即返回0,成功被执行)后,&&右边的命令(命令2)才能够被执行;换句话说,“如果这个命令执行成功&&那么执行这个命令”。 

 

||则与&&相反。如果||左边的命令(命令1)未执行成功,那么就执行||右边的命令(命令2);或者换句话说,“如果这个命令执行失败了||那么就执行这个命令。

 

 

  • ()运算符:
为了在当前shell中执行一组命令,可以用命令分隔符(即",")隔开每一个命令,并把所有的命令用圆括号()括起来。 
它的一般形式为: 
        ( 命令;命令;命令… )
 
1 一条命令需要独占一个物理行,如果需要将多条命令放在同一行,命令之间使用命令分隔符(;)分隔。执行的效果等同于多个独立的命令单独执行的效果。
2 () 表示在当前 shell 中将多个命令作为一个整体执行。需要注意的是,使用 () 括起来的命令在执行前面都不会切换当前工作目录,也就是说命令组合都是在当前工作目录下被执行的,尽管命令中有切换目录的命令。
3 命令组合常和命令执行控制结合起来使用。
 
 
{}运算符:
如果使用{}来代替(),那么相应的命令将在子shell而不是当前shell中作为一个整体被执行,只有在{}中所有命令的输出作为一个整体被重定向时,其中的命令才被放到子shell中执行,否则在当前shell执行。
它的一般形式为: 
      { 命令;命令;命令… } 
(注意:在使用{}时,{}与命令之间必须使用一个空格)