标签:style io ar color os 使用 sp for on
什么是脚本,其实脚本是有若干个命令串起来执行的命令行,如下图。命令行一行有255个字符数的限制,所以一般长些的脚本都写在文件中。
[root@localhost ~]# date;uname -a;pwd
Thu Dec 4 17:29:52 CST 2014
date命令显示Linux localhost.localdomain 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
uname -a命令显示/root
pwd命令显示
[root@localhost ~]# cat test.sh
#!/bin/bash
Shebang这个符号通常在Unix系统的脚本中第一行开头中写到,它指明了执行这个脚本文件的解释程序A=
test
test赋值给Aecho "$A" 双引号是弱引用,显示A变量
echo ‘$A‘
单引号是强引用,所以显示的是$A[root@localhost ~]# bash test.sh bash是命令,test.sh相当于参数
test
$A
[root@localhost ~]# A=`pwd`
命令引用,把命令结果输出[root@localhost ~]# echo $A
/root
[root@localhost ~]# ./test.sh
shell能够提供一些内部命令,并且能通过PATH环境变量找到外部命令;把命令提交给内核启动为进程,由于被当成命令来执行没有执行权限,所有被拒绝-bash: ./test.sh: Permission denied
[root@localhost ~]# ll test.sh
-rw-r--r-- 1 root root 39 Dec 5 10:49 test.sh
[root@localhost ~]# chmod a+x test.sh
[root@localhost ~]# ll test.sh
-rwxr-xr-x 1 root root 39 Dec 5 10:49 test.sh
[root@localhost ~]# ./test.sh
bash读取到了第一行的shebang,就调用bashtest
$A
#!/bin/bash
count1=1
for i in "$*"; do
echo "A$count1 = $*"
count1=$[ $count1 + 1 ]
done
count2=1
for j in "$@"; do
echo "B$count2 = $@"
count2=$[ $count2 + 1 ]
done
[root@localhost ~]# bash test.sh 1 2
A1 = 1 2
B1 = 1 2
B2 = 1 2
[root@localhost ~]# A=1
[root@localhost ~]# echo $[ $A+1 ] 1、第一种算术运算
3
[root@localhost ~]# echo $[$A+1]
3
[root@localhost ~]# echo $(($A+1)) 2、第二种算术运算
3
[root@localhost ~]# let A++ 3、自增相当于let A=$A+1
[root@localhost ~]# echo $(( $A+1 ))
3
[root@localhost ~]# echo $( expr $A+1) 4、必须在+号两边那空格
2+1
[root@localhost ~]# echo $( expr $A + 1)
3
[root@localhost ~]# ls /etc/fstab /1111 2> test2 1> test3 如不重定向默认是在显示器上输出
[root@localhost ~]# cat test2 将STDERR错误重定向到test2
ls: cannot access /1111: No such file or directory
[root@localhost ~]# cat test3 将STDOUT重定向到test3
/etc/fstab
[root@localhost ~]# ls /etc/fstab /1111 &> test4 把正确的和错误的都重定向到test4
[root@localhost ~]# cat test4
ls: cannot access /1111: No such file or directory
/etc/fstab
bash test.sh
root@localhost ~]# cat test.sh
#!/bin/bash
exec 2>testerror
exec 1>testout
echow "error"
echo "aaa"
lss /tmp
ls /tmp
[root@localhost ~]# cat testout
aaa
test
[root@localhost ~]# cat testerror
test.sh: line 5: echow: command not found
test.sh: line 7: lss: command not found
#!/bin/bash
exec 3>test3out
echo "11"
echo "22"
echo "33" >&3 >&重定向到 <&输入重定向
[root@localhost test]# bash test.sh
11
22
[root@localhost test]# cat test3out
33
[root@localhost ~]# pwdd;echo $? 命令执行失败后状态码为127
-bash: pwdd: command not found
127
[root@localhost ~]# pwd;echo $? 命令执行成功,后显示为0
/root
0
- [root@localhost ~]# cat test.sh 可以通过exit来定义退出状态码
- #!/bin/bash
- exit 300
- [root@localhost ~]# echo $?
- 44
[root@localhost ~]# cat > test6 << EOF 在屏幕输出后输入到test6
> first
> second
> third
> EOF
[root@localhost ~]# cat test6
first
second
third
[root@localhost ~]# [ 1 -ne 0 ];echo $?
1不等于0为真0
[root@localhost ~]# [ 1 -eq 0 ];echo $?
1等于0为假1
[root@localhost ~]# [[ 10 -eq 9 ]];echo $?
10等于9为假1
[root@localhost ~]# [[ 10 -ne 9 ]];echo $?
10不等于9为真0
[root@localhost ~]# test 1 -ne 0;echo $?
1不等于0为真0
[root@localhost ~]# test 1 -eq 0;echo $?
1等于0为假1
[root@localhost ~]# [[ a > b ]];echo $? a大于b为假
1
[root@localhost ~]# [[ a < b ]];echo $?
a小于b为真0
[root@localhost ~]# [ a < b ];echo $? 在字符测试中单中括号是不生效的
0
[root@localhost ~]# [ a > b ];echo $?
0
- [root@localhost ~]# test a > b;echo $?
0
[root@localhost ~]# test a < b;echo $?
0
[root@localhost ~]# test a \< b;echo $?
字符比较大于等于号必须要加反斜杠转义0
[root@localhost ~]# test a \> b;echo $?
1
[root@localhost ~]# echo $A
blue
[root@localhost ~]# [[ "$A" =~ b ]];echo $?
0
[root@localhost ~]# [[ "$A" =~ a ]];echo $?
1
[root@localhost ~]# [[ "$A" =~ a* ]];echo $?
0
[root@localhost ~]# [[ "$A" =~ . ]];echo $?
0
[root@localhost ~]# [[ "$A" =~ * ]];echo $?
2
[root@localhost ~]# [[ -b /dev/sda ]];echo $?
0
[root@localhost ~]# [[ -b /dev/sd ]];echo $?
1
[centos@localhost ~]$ [[ -w /etc/passwd ]];echo $? centos用户对/etc/passwd文件不能写
1
[centos@localhost ~]$ [[ -w /etc/ ]];echo $? 也可以对目录测试
1
[centos@localhost ~]$ [[ -w /tmp ]];echo $?
0
[centos@localhost ~]$ touch /tmp/centos
[centos@localhost ~]$ ll /tmp/centos
-rw-rw-r-- 1 centos centos 0 Dec 5 14:37 /tmp/centos
[centos@localhost ~]$ [[ -w /tmp/centos ]];echo $? 可以写显示为0
0
[centos@localhost ~]$ ll /etc/fstab
-rw-r--r--. 1 root root 921 Nov 28 08:29 /etc/fstab
[centos@localhost ~]$ ll /tmp/test.sh
-rw-r--r-- 1 root root 339 Dec 5 10:49 /tmp/test.sh
[centos@localhost ~]$ [[ /etc/fstab -nt /tmp/test.sh ]];echo $?
1
[centos@localhost ~]$ [[ /etc/fstab -ot /tmp/test.sh ]];echo $? fstab比test旧
0
[root@localhost ~]# [ -z "" -a -f /etc/fstab ];echo $? 2者都满足则为真
0
[root@localhost ~]# [ -n "" -a -f /etc/fstab ];echo $?
1
[root@localhost ~]# [ 1 -gt 2 ];echo $? 没加!状态码非0
1
[root@localhost ~]# [ ! 1 -gt 2 ];echo $? 加了!状态码为0
0
- 在/tmp/下创建了4个文件,以下真为状态码为0
- 真 执行 && 真 执行 && 真 执行 && 真 执行
[root@localhost ~]# ls /tmp/test1 && ls /tmp/test2 && ls /tmp/test3 && ls /tmp/test4
/tmp/test1
/tmp/test2
/tmp/test3
/tmp/test4
- 真 执行 && 真 执行 || 不执行 && 真执行
[root@localhost ~]# ls /tmp/test1 && ls /tmp/test2 || ls /tmp/test3 && ls /tmp/test4
/tmp/test1
/tmp/test2
/tmp/test4
- 真 执行 || 不执行 || 不执行 && 执行
[root@localhost ~]# ls /tmp/test1 || ls /tmp/test2 || ls /tmp/test3 && ls /tmp/test4
/tmp/test1
/tmp/test4
- 假 返回错误 || 真 执行 || 不执行 && 执行
[root@localhost ~]# lss /tmp/test1 || ls /tmp/test2 || ls /tmp/test3 && ls /tmp/test4
-bash: lss: command not found
/tmp/test2
/tmp/test4
[root@localhost ~]# ls /tmp/test1 &> /dev/null;echo $? 命令执行成功状态码为0,&> /dev/null 为把错误和正常输出全部丢弃
0
[root@localhost ~]# ! ls /tmp/test1 &> /dev/null;echo $? 加了!状态码为非0
1
[root@localhost ~]# read -t 3 -p "Enter your menu:" menu -t为3秒后不等待输入,直接进入下一步 -p为显示输入提示,
Enter your menu:
menu为输入的参数赋值给menu
[root@localhost ~]# A=test;echo ${A:2:2} 取3,4两个字符
st
[root@localhost ~]# A=test;echo ${A: -2}
st
[root@localhost ~]# A=www.sina.com.cn;echo ${A#*.}
sina.com.cn
[root@localhost ~]# A=www.sina.com.cn;echo ${A##*.}
cn
[root@localhost ~]# A=www.sina.com.cn;echo ${A%.*}
www.sina.com
[root@localhost ~]# A=www.sina.com.cn;echo ${A%%.*}
www
[root@localhost ~]# A="aaa bb cc aa";echo ${A/a*/AA}
AA
[root@localhost ~]# A="aaa bb cc aa";echo ${A/a?/AA}
AAa bb cc aa
[root@localhost ~]# A="aaa bb cc aa";echo ${A//a?/AA}
AAAAbb cc AA
[root@localhost ~]# A="aaa bb cc aa";echo ${A/#a?/AA}
AAa bb cc aa
[root@localhost ~]# A="aaa bb cc aa";echo ${A/#b*/AA}
aaa bb cc aa
[root@localhost ~]# A="aaa bb cc aa";echo ${A/%cc/AA}
aaa bb cc aa
[root@localhost ~]# A="aaa bb cc aa";echo ${A/%aa/AA}
aaa bb cc AA
[root@localhost ~]# A="aaa bb cc aa";echo ${A/b*}
aaa
[root@localhost ~]# A="aaa bb cc aa";echo ${A/b?}
aaa cc aa
[root@localhost ~]# A="aaa bb cc aa";echo ${A//a?}
bb cc
[root@localhost ~]# A="aaa bb cc aa";echo ${A//a*}
[root@localhost ~]# A="aaa bb cc aa";echo ${A/#a?}
a bb cc aa
[root@localhost ~]# A="aaa bb cc aa";echo ${A/%a?}
aaa bb cc
[root@localhost ~]# A="aa";echo ${A^^}
AA
[root@localhost ~]# A="AA";echo ${A,,}
aa
[root@localhost ~]# echo $A
[root@localhost ~]# echo ${A:-test}
test
[root@localhost ~]# echo $A
[root@localhost ~]# echo $A
[root@localhost ~]# echo ${A:=test}
test
[root@localhost ~]# echo $A
test
[root@localhost ~]# echo $A
[root@localhost ~]# echo ${A:?test}
-bash: A: test
[root@localhost ~]# echo $A
[root@localhost ~]# A=tset
[root@localhost ~]# echo ${A:?111}
tset
[root@localhost ~]# echo $A
[root@localhost ~]# echo ${A:+test}
[root@localhost ~]# echo $A
[root@localhost ~]# A=AAAAA
[root@localhost ~]# echo ${A:+test}
test
[root@localhost ~]# echo $A
AAAAA
标签:style io ar color os 使用 sp for on
原文地址:http://www.cnblogs.com/kwstars/p/f4cc93aac5ebd690c2bc9bb6f76b6348.html