shell脚本之特殊命令

exec 在脚本里正确错误输出,有时候执行命令后会输入到桌面上,可以使用exec命令把正确和错误信息分别重定向到一个文件,也可以多个文件:

exec 1>  正确重定向:

exec 1>>  正确追加重定向

exec  2>   错误重定向:

exec  2>>  错误追加重定向:

[root@localhost_01 shell]# cat test.sh 
#!/bin/bash
exec 1>>/tmp/1.log   2>>/tmp/2.log
ls
touch 123
date
sdafsadfkl;j;lkj
sdkjfskd;lfsdf
skdjf;sladf
[root@localhost_01 shell]# sh test.sh 
[root@localhost_01 shell]# cat /tmp/1.log 
1
fun3.sh
fun4.sh
fun5.sh
fun6.sh
fun7.sh
fun8.sh
funciton1.sh
function.sh
test.sh
Sat Sep 29 19:36:55 CST 2018
[root@localhost_01 shell]# cat /tmp/2.log 
test.sh: line 6: sdafsadfkl: command not found
test.sh: line 6: j: command not found
test.sh: line 6: lkj: command not found
test.sh: line 7: sdkjfskd: command not found
test.sh: line 7: lfsdf: command not found
test.sh: line 8: skdjf: command not found
test.sh: line 8: sladf: command not found

注释:

2、date   打印日期

3、read  -p    shell中用于输入字符:也用来做判断:

4、mkpasswd:用于生成字符串:   可用来脚本中随机生成文件名,   或者是作为用户的密码:

安装:    yum    install   expect

-s: 不含有特殊符号:      -s  0      (只包括小写大写和数字)    ===  spcial

-l:指定长度:   -l    10

-c:指定小写:   -c   0   表示不含有小写的:

-d:指定数字:  -d   0   表示不含有数字的:

-C:指定大写:  -C 10:

[root@localhost_01 ~]# mkpasswd -s 0 -l 10           #包含大小写字母和数字,不包括符合哟:
Rrlnk4bA3h 
[root@localhost_01 ~]# mkpasswd -l 10 -s 0 -c 0 -d 0 -C 10
IIOYMDQZLN

5、test:常用在逻辑判断的一个条件上,用来代替if判断的中括号[]:

if  [ -r  /tmp/1.sh ]   ======     if   test -r  /tmp/1.sh

[root@localhost_01 shell]# cat test1.sh 
#!/bin/bash
filename=/root/shell/123
if test -f $filename
then
   echo $filename is exist;
else
   echo $filename no exists;
fi
[root@localhost_01 shell]# sh test1.sh 
/root/shell/123 is exist

6、sleep  休眠:   反复执行一条命令,会用于监控系统性能,出现死循环 ---- 判断 ---- sleep  30

#!/bin/bash
while true
do 
    echo 123;
    sleep 5
done
[root@localhost_01 shell]# sh test2.sh 
123
123
123

注释:表示每个5秒打印一次123:      

注释:while true =====    while   :

7、break:中断循环,跳出本次循环:如下图:整个for循环里面的内容都不会再执行:

[root@localhost_01 shell]# cat test3.sh 
#!/bin/bash
for i in `seq 1 5`
do 
    echo $i
    if [ $i == 3 ]
    then
        break
    fi
    echo $i
done
echo end
[root@localhost_01 shell]# sh test3.sh 
1
1
2
2
3
end

continue:遇到continue后,只是continue下的命令不执行,然后再开始下面的循环:如下:

[root@localhost_01 shell]# cat test4.sh 
#!/bin/bash
for i in `seq 1 5`
do 
    echo $i
    if [ $i == 3 ]
    then
       continue
    fi
    echo $i
done
echo end
[root@localhost_01 shell]# sh test4.sh 
1
1
2
2
3
4
4
5
5
end

注释:如上,continue表示只是continue下的内容不打印,然后继续打印下面的内容:

7、echo  -n和echo  -e

echo  -n:表示输入不换行:         echo  -n 111 ; echo -n 222

[root@localhost_01 ~]# echo 111 ; echo 222
111
222
[root@localhost_01 ~]# echo -n 111 ; echo -n 222
111222[root@localhost_01 ~]# 

echo  -e:可以使用换行符 -n 和 tab (-t)    echo  -e  111\n222                 echo -e 111\t222

[root@localhost_01 ~]# echo -e "111\n222"
111
222
[root@localhost_01 ~]# echo -e "111\t222"
111	222

8、嵌入文档 cat <<EOF

:在脚本里再写入脚本:如下:   使用echo  -e,  后面是-n换行符,也需要脱义的:

[root@localhost_01 shell]# cat test5.sh 
#!/bin/bash
echo -e "for i in \`seq 1 5\`\ndo\n     echo \$i\ndone" >/tmp/test.sh
[root@localhost_01 shell]# sh test5.sh
[root@localhost_01 shell]# cat /tmp/test.sh 
for i in `seq 1 5`
do
     echo $i
done

有时这样写太繁琐,是否可以改进:使用EOF格式:  cat   <<  EOF > /tmp/test1.sh     内容..........        EOF

[root@localhost_01 shell]# cat  test5.sh 
cat << EOF > /tmp/test1.sh
#!/bin/bash
for i in \`seq 1 5\`
do
     echo $i
done
EOF
[root@localhost_01 shell]# sh test5.sh 
[root@localhost_01 shell]# cat /tmp/test1.sh 
#!/bin/bash
for i in `seq 1 5`
do
     echo 
done

注释:从EOF开头到最后的EOF结尾时,中间的内容都是嵌入内容,会定向到定义的例外文件中,有时候写脚本中,脚本中又包含子脚本:

cat   << EOF > /tmp/test1.sh                               EOF

转载于:https://my.oschina.net/yuanhaohao/blog/2221693

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值