linux Shell Note

----------------------------------------------------------------------
*.Shell 语法
------------------------------------------------------------------------
 不用声明变量,通常所有的变量被认为是以string格式保存,甚至是整
型变量,shell会自动转换成其想要的值类型
 $ salutation = Hello
 $ echo $salutation
 
 $ salutation = "Leon Chang"
 $ echo $salutation

 $ salutation = 7+5
 $ echo $salutation
 7+5
 #接收用户输入,打印输出
 $ read salutation
 Hello LeonChang
 $ echo $salutaion

 上面引号""的作用是可以包含空格,"$salutaion"还是显示其值, 但是如果是'$salutaion',\$salutaion
shell并不会打印salutation的值,而是直接输出$salutaion(转译字符)

 

 

 

 

$IFS,作为输入域分离器,一些字符如空格,tab,换行符用来分离用户输入

$ IFS=’’
$ set foo bar bam    #向变量IFS复制
$ echo “$@”               #更明智的选择
   foo bar bam
$ echo “$*”
   foobarbam
$ unset IFS
$ echo “$*”
   foo bar bam

 

 

 

2参数变量脚本
      功能:“获得脚本后面的参数信息”
      准备:   $0 ,输出脚本的名称
        $1, 得到第一个变量,$2得到第二个变量
        $*, 得到变量列表
        $HOME, 得到用户home路径
      脚本:
      1. 新建文件名try_var, 更改文件属性为所有用户可执行
          $ chmod +x try_var
      2. 添加脚本
        #!/bin/sh
        salutation=”Hello”
        echo $salutation
        echo “The program $0 is now running”
        echo “The second parameter was $2”
        echo “The first parameter was $1”
        echo “The parameter list was $*”
        echo “The user’s home directory is $HOME”
        echo “Please enter a new greeting”
        read salutation
        echo $salutation
        echo “The script is now complete”
        exit 0

      3. 执行 

        ./trt_var   foo   bar   baz

 

 

 

 

3.条件变量脚本
   1.功能:”检查一个文件是否存在“
      准备:调用  test  -f <filename>      //Find the file
      脚本:
    if test -f fred.c          #f -- find
    then
        echo "找到该文件"
    fi 
或者
    if [  -f  fred.c ]
    then
        echo "找到该文件"
    fi


   2.功能:测试文件状态
    脚本:
#!/bin/sh

if [ -f  /bin/bash ]      #省略了test 关键字
then
     echo "file /bin/bash exists"
fi

if [  -d /bin/bash ]     #test -d 判断是否是路径
then
     echo "/bin/bash is a directory"
else
     echo "/bin/bash is not a directory"
fi

 

 

 


4.控制结构脚本
 1.if-then elif-then else fi格式:
 if condition
 then
          statements
 else  // elif then 结构添加
          statements
 fi
脚本:
#!/bin/sh
echo “Is it morning? Please answer yes or no”
read timeofday
if [ $timeofday = “yes” ]; then       #最好加上“”,"$timeofday"避免出现   = "yes"错误表达
echo “Good morning”
else
echo “Good afternoon”
fi
exit 0

 

 

 

5. for 结构
for variable in values
do
     statements
done

 

脚本:
#!/bin/sh

for    tmp   in    bar fun 43     #依次遍历bar ,fun ,43,并打印输出
do
     echo $tmp
done
exit 0
---------------
脚本:
#!/bin/sh

for   file   in  $(ls f*.sh);
do
     lpr  $file                  #lpr---- print 打印输出
done
exit 0

 

 

 

6.while 结构脚本
描述:
   读入用户输入密码,判断是否通过
格式:
while condition
do
     statements
done


脚本:
#!/bin/sh

echo "Enter password"
read  trythis

while [ "$trythis" != "secret" ];
do
     echo "Sorry, try again!"
     read trythis
done
exit  0

补充:
 foo = $((  $foo+1  ))       对变量的值操作是用到$,

 

 

 

 

4.until 结构脚本
#!/bin/sh
until    who | grep  "$1" > /dev/null        #who--- 显示目前登录系统的用户
do
     sleep 60
done

#宣布有人进入
echo   -e   \\a
echo   "***********$1 has just logged in*************"

exit   0

 

 

 

 

 

5.case结构
#!/bin/sh
echo “Is it morning? Please answer yes or no”
read timeofday

case “$timeofday” in
 yes ) echo “Good Morning”;;
 no    ) echo “Good Afternoon”;;
 y | YEAH ) echo “Good Morning”;;
 n | NOT ) echo “Good Afternoon”;;
 *  ) echo “Sorry, answer not recognized”;;
esac
exit 0

注: 条件中, [Nn]* 表示了No,Not,no,not...etc

 

 

 

 

 

posted @ 2010-11-25 09:14  hungryMan  阅读(293)  评论(0编辑  收藏  举报