Shell command 使用习惯和语句特点

来源:互联网 发布:js getelementsbyname 编辑:程序博客网 时间:2024/06/05 09:55

执行shell script 的方法
   -----sh …
   -----直接授权x权限
一般先用sh...调试,没问题了,再改权限


   shell script 调试
sh -x test.sh
sh -vx test.sh  


    shell语句的三大格式特点

  • 空格——所有关键字,变量,运算符之间必须都有空格(赋值符号除外),否则shell会连读
  • 无大括号——多语句组合,没有大括号{ },因为if,case,while等语句都有开始和结束关键字如if   fi, do    done, case    esac
  • 无括号——计算式没有括号( ),并列操作符之间的优先执行次序由系统自动掌握



   以#!开头的行,是特殊的注释行,必须位于script第一行。明确使用什么SHELL来翻译脚本
   如果#! 不位于第一行(比如开头空行),就会出错

$ vi autologin
开头有空行,不良的习惯

#!/usr/bin/expect      #!不在开头行,就会被当作普通的注释

proc do_console_login {login pass}
{  
下面依赖此行的expect语句,就会出错
$ ./autologin macg 008421
./autologin: line 4: proc: command not found    
系统不认识expect语句proc ,说明#!/usr/bin/expect没起作用$ vi autologin
#!/usr/bin/expect

proc do_console_login {login pass} {    
删掉空行,使#!在第一行,问题解决


 

   shell指令与C语言一大区别,指令没有“;”
   shell语句虽然没有分号;但也有用分号的地方,就是多个命令一行,通常是一些条件语句或循环语句
下面两列的指令是等价的,就是回车另起一行和同行多命令的区别

if [ $# -lt 3 ] ;then
if [ $# -lt 3 ]
then
while [ -n "$1" ]; do
while [ -n "$1" ]
do
case $1 in
  -h) help;shift 1;;  
case $1 in
   -h) help
      shift 1;;



   
    If  while 等Shell语句下可以含多条command,这些command之间没有分号,也没有总的{ }
If …
then
echo "server is up"               
rm /tmp/server.tmp
rm /tmp/server1.tmp
else
echo "server is not up"
/etc/init.d/httpd start
rm /tmp/server.tmp
rm /tmp/server1.tmp
fi


     shell的变量编程习惯
   正常UNIX命令引用变量,都带$

echo "file $old is now called $new /c"     echo 引号内引用变量,也要用$
mv $old $new
touch $filename

    变量不带$的特殊情况:read、赋值= 、export不是unix命令,是shell命令,所以引用变量,不带$

read old   
old="xxx"
export PATH

   所有字符串之类的都用变量,避免直接引用字符串

file=’tmp.log’
touch $file

    尽量多用shell 环境变量

HOME="/root"
HOSTNAME="mac-home"
REMOTEHOST=192.168.1.100
USER="macg"
LOGNAME="macg"
PATH="/usr/kerberos/sbin:/usr/kerberos/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/macg/bin"
PWD="/home/macg"
SHELL="/bin/bash"
TERM="vt100"

    环境变量和系统变量也尽量避免直接引用,先赋值到普通变量

vi ttt.sh
ttt=$REMOTEHOST
echo $ttt

$ sh ttt.sh
192.168.1.11
ttt=$?
If  [ $ttt == 0] 



    几个command常调用的if 条件

 if [ !-f /tmp/test.tmp ]; then
 touch /tmp/test.tmp 如果文件不存在
shell建文件前,都要先-f ..  后touch或mknod if [ !-d /tmp/test ]; then
  mkdir test  如果目录不存在
shell建目录前,都要先-d if [ !-s /tmp/test.tmp ]; then
  more /tmp/test.tmp
 如果文件存在且size非0
shell读写文件前,都要先-s if [-z $str ]; then
  read str
 如果字符串为0 if [-n $str ]; then
 echo $str 如果字符串非0
shell 操作字符串前,都要先-n



    shell所有关键字,运算符号,操作符号,变量,都必须分开,shell没有连读识别功能

    以If 条件表达式为例,显示空格要求

  • if 条件 的中括号必须和其内的表示式以空格隔开,否则会被系统和后面字符读在一起
[root@mac-home macg]# vi test.sh
if [-d $num]
then
cd $num
fi

[root@mac-home macg]# sh ./test.sh
input :
ppp
the input data is ppp
./test.sh: line 6: [-d: command not found
[root@mac-home macg]# vi test.sh
if [ -d $num]
then
cd $num
fi

[root@mac-home macg]# sh ./test.sh
input :
ppp
the input data is ppp
./test.sh: line 6: [: missing `]'[root@mac-home macg]# vi test.sh
if [ -d $num ]
then
cd $num
fi
  •    所有各类if 条件符号,之间都必须有空格,否则系统读到一块去了
if [ !-d $num ]
then
mkdir $num
test.sh: line 6: [: !-d: unary operator expected 
if [ ! -d $num ]                   
then
mkdir $num
ls -l
else
echo "will issue ls -d $num"
ls -d $num
fi

[root@mac-home macg]# sh test.sh
input num:
ppp
 input is  ppp
will issue ls -d ppp
ppp
  •  If 和[之间也必须有空格[macg@mac-home ~]
$vi test.sh
:
if[ $ANS …. ]

test.sh: line 6: if[: command not found

 

  •    赋值语句正好相反,abc=9 (bash/pdksh,不能在等号两侧留下空格 )


原创粉丝点击