Shell command 使用习惯和语句特点
2011-01-27 14:28
302 查看
执行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来翻译脚本
如果#! 不位于第一行(比如开头空行),就会出错
shell指令与C语言一大区别,指令没有“;”
shell语句虽然没有分号;但也有用分号的地方,就是多个命令一行,通常是一些条件语句或循环语句
下面两列的指令是等价的,就是回车另起一行和同行多命令的区别
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命令引用变量,都带$
变量不带$的特殊情况:read、赋值= 、export不是unix命令,是shell命令,所以引用变量,不带$
所有字符串之类的都用变量,避免直接引用字符串
尽量多用shell 环境变量
环境变量和系统变量也尽量避免直接引用,先赋值到普通变量
几个command常调用的if 条件
shell所有关键字,运算符号,操作符号,变量,都必须分开,shell没有连读识别功能
以If 条件表达式为例,显示空格要求
if 条件 的中括号必须和其内的表示式以空格隔开,否则会被系统和后面字符读在一起
所有各类if 条件符号,之间都必须有空格,否则系统读到一块去了
If 和[之间也必须有空格[macg@mac-home ~]
赋值语句正好相反,abc=9 (bash/pdksh,不能在等号两侧留下空格 )
-----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语句虽然没有分号;但也有用分号的地方,就是多个命令一行,通常是一些条件语句或循环语句
下面两列的指令是等价的,就是回车另起一行和同行多命令的区别
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 …
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 old old="xxx" export PATH |
file=’tmp.log’ touch $file |
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] |
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 |
以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 [ !-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 |
$vi test.sh : if[ $ANS …. ] test.sh: line 6: if[: command not found |
相关文章推荐
- Shell command 使用习惯和语句特点
- shell 编程中使用到得if语句内判断参数
- .NET动态生成DataSet下使用DataAdapter保存数据时,如何生成command语句及使用事务(转)
- 使用Adb shell command直接送key event 给 Android
- 使用EntityFramework6.1的DbCommandInterceptor拦截生成的SQL语句
- 使用Adb shell command直接送key event給Android
- nios II command shell 的使用
- 使用shell的if else fi语句和注意的地方
- 使用SAPCommand查询SAP表的SELECT语句
- 如何在Windows下开发Python:在cmd下运行Python脚本+如何使用Python Shell(command line模式和GUI模式)+如何使用Python IDE
- 使用shell脚本生成*Db.java、*.thrift或*.yaml文件语句
- 使用Command与Factory模式消除业务代码中的if,else语句
- Linux入门 Part1: 使用控制台(3)-shell,command & parameter(s)
- android 使用shell command实现对设备控制,实现按坐标点击
- shell脚本编程:条件判断if语句使用小结
- 使用for、if语句添加用户的一个shell脚本
- sed 命令的使用及shell判断语句
- 使用Adb shell command直接送key event給Android
- 不完全整理Makefile中的shell语句使用(仅针对实用为主)
- 使用OleDbCommandBuilder.GetInsertCommand时出现“Insert into 语句的语法错误”