shell脚本之特殊命令
作者:佚名 来源:哪吒游戏网 2019-12-10 20:15:31
shell脚本之特殊命令,哪吒游戏网给大家带来详细的shell脚本之特殊命令介绍,大家可以阅读一下,希望这篇shell脚本之特殊命令可以给你带来参考价值。

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 
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 ~]# 总结:以上内容就是针对shell脚本之特殊命令详细阐释,如果您觉得有更好的建议可以提供给哪吒游戏网小编,shell脚本之特殊命令部分内容转载自互联网,有帮助可以收藏一下。
上一篇: linux 常用命令整理 -- 特殊命令(五) -- 持续更新
- 1 魔兽世界 考古(魔兽世界考古毁一生?这些装备幻化和坐骑值得你去玩考古)
- 2 普罗霍洛夫(卢布危机下俄土豪大甩卖 卖完豪宅卖球队)
- 3 龙之谷手柄(《龙之谷手游》手柄怎么连接 柄连接教学攻略)
- 4 普罗霍洛夫(俄罗斯土豪准备20亿抛售篮网! 最烂老板是怎样炼成的?)
- 5 天联网(天联网信息科技有限公司怎么样?)
- 6 附魔大师(魔兽世界怀旧服附魔大师在哪 附魔大师位置分享介绍)
- 7 wow烹饪食谱(魔兽世界怀旧服烹饪极品食谱)
- 8 陶谦让徐州(陶谦三让徐州,世界上真有这样的好人吗?)
- 9 lol神圣之剑(LOL如果神圣之剑回归,谁最受益?第1:只要不瞎都能上钻石!)
- 10 陶谦让徐州(陶谦三让徐州的原因是什么?)

机械战警
坦克射击
梦道满V版
火箭精英3d免费版
太古灵诀
小小帝国无敌破解版
厉害了我的娃
乐高无限
侠影双剑九游版