Shell command 使用习惯和语句特点
来源:互联网 发布:淘宝售后工作职责 编辑:程序博客网 时间:2024/06/05 11:44
执行shell script 的方法
-----sh …
-----直接授权x权限
一般先用sh...调试,没问题了,再改权限
shell script 调试
sh -x test.sh
sh -vx test.sh
shell语句的三大格式特点
以#!开头的行,是特殊的注释行,必须位于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 ] ;thenif [ $# -lt 3 ]
thenwhile [ -n "$1" ]; dowhile [ -n "$1" ]
docase $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.11ttt=$?
If[ $ttt == 0]
几个command常调用的if 条件
if [ !-f /tmp/test.tmp ]; then
touch /tmp/test.tmp如果文件不存在
shell建文件前,都要先-f ..后touch或mknodif [ !-d /tmp/test ]; then
mkdir test如果目录不存在
shell建目录前,都要先-dif [ !-s /tmp/test.tmp ]; then
more /tmp/test.tmp如果文件存在且size非0
shell读写文件前,都要先-sif [-z $str ]; then
read str如果字符串为0if [-n $str ]; then
echo $str如果字符串非0
shell 操作字符串前,都要先-n
shell所有关键字,运算符号,操作符号,变量,都必须分开,shell没有连读识别功能
以If 条件表达式为例,显示空格要求
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
then
mkdir $num
test.sh: line 6: [: !-d: unary operator expectedif [ ! -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[ $ANS …. ]
test.sh: line 6: if[: command not found
一般先用sh...调试,没问题了,再改权限
sh -x test.sh
sh -vx test.sh
- 空格——所有关键字,变量,运算符之间必须都有空格(赋值符号除外),否则shell会连读
- 无大括号——多语句组合,没有大括号{ },因为if,case,while等语句都有开始和结束关键字如if
fi, do done, case esac - 无括号——计算式没有括号( ),并列操作符之间的优先执行次序由系统自动掌握
$ vi autologin
开头有空行,不良的习惯
#!/usr/bin/expect
proc do_console_login {login pass}
{
$ ./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} {
删掉空行,使#!在第一行,问题解决
下面两列的指令是等价的,就是回车另起一行和同行多命令的区别
if [ $# -lt 3 ] ;thenif [ $# -lt 3 ]
thenwhile [ -n "$1" ]; dowhile [ -n "$1" ]
docase $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
echo "file $old is now called $new \c"
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.11ttt=$?
If
shell建文件前,都要先-f ..
shell建目录前,都要先-d
shell读写文件前,都要先-s
shell 操作字符串前,都要先-n
- if 条件 的中括号必须和其内的表示式以空格隔开,否则会被系统和后面字符读在一起
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 条件符号,之间都必须有空格,否则系统读到一块去了
then
mkdir $num
test.sh: line 6: [: !-d: unary operator expected
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
will issue ls -d ppp
ppp
If 和[之间也必须有空格[macg@mac-home ~]
:
if[ $ANS …. ]
test.sh: line 6: if[: command not found
赋值语句正好相反,abc=9 (bash/pdksh,不能在等号两侧留下空格 )
- Shell command 使用习惯和语句特点
- Shell command 使用习惯和语句特点
- shell--条件测试语句和流程控制语句的使用
- Pyonth特点和使用
- 【编程好习惯】恰当使用goto语句
- 使用remote shell来执行command
- shell脚本:使用if-then语句和test命令
- command shell(flume command)
- shell command
- ARC 使用特点和注意事项
- Memcached的特点和使用
- scanf()和getchar() 使用特点
- shell条件语句的使用
- shell脚本之-if流程控制语句和for循环语句的使用
- shell中的if语句和case语句
- shell中的循环语句和条件语句
- shell while语句和until语句
- Python编程习惯与特点
- shell变量
- 【转】source insight 快捷键 汇总
- js鼠标点击展开收起特效(带缓动效果)
- C/S和B/S
- Excel数据导入至数据库
- Shell command 使用习惯和语句特点
- 3Sum Closest
- OpenStack对象存储-swift
- c语言版数据结构(奇迹冬瓜)-链表实战(2)合并两有序线性表
- 将代码以Eval方式加密、解密
- 常用的Double保留N位小数的转换
- RemoteViews的使用
- shell编程——循环语句
- JAVA序列化与反序列化三种格式存取(默认格式、XML格式、JSON格式)