shell脚本之重定向、标准输入、输出、错误输出的使用

linux    shell脚本

   

大量重复执行的工作 

shell (Linux壳), 一类程序的名称


文本文件-------> shell命令, /bin/bash提供逻辑控制语句, 



重定向符号的使用 


/dev/stdin 标准输入设备(键盘) 0

/dev/stdout 标准输出设备(显示器) 1

/dev/stderr 标准错误输出设备(显示器) 2


输出重定向符号


> >> 用于重定向标准输出

[root@shell ~]# ls -ldh /etc/ > /tmp/1.txt

[root@shell ~]# ls -ldh /tmp/ >> /tmp/1.txt 


2>  2>> 用于重定向标准错误输出 

[root@shell ~]# ls -ldh /asjdfklasjdlfasd 2> /tmp/1.txt 



&> 同时重定向标准输出及标准错误输出 


特殊设备文件: /dev/null 


[root@shell ~]# ls -ldh /etc/ &> /dev/null 



输入重定向符号


[root@shell ~]# tr 'a-z' 'A-Z' < /tmp/1.txt 

ABCDEF

[root@shell ~]# 

[root@localhost ~]# mail -s "test fstab" root@localhost < /etc/fstab 





输出信息:


1 echo 


[root@shell ~]# echo "请输入你的选择:" 默认会打印换行符

请输入你的选择:


[root@shell ~]# echo -n "请输入你的选择:"

请输入你的选择:[root@shell ~]# 


[root@shell ~]# echo -e "a\nbb\nccc" \n:回车   或者\r

a

bb

ccc

[root@shell ~]# echo -e "a\tbb\tccc" \t tab键

a bb ccc

[root@shell ~]# 



2 printf


[root@shell ~]# printf "hello world"

hello world[root@shell ~]# 



3 HERE DOCUMENT -----> 输出多行内容 


[root@shell ~]# cat << eof

> 1. 安装KVM

> 2. 重置所有虚拟机

> eof

1. 安装KVM

2. 重置所有虚拟机

[root@shell ~]# 



双引号和单引号的区别:


单引号: 所有字符会失云原有的含义 

双引号: 特殊字符会被转义




如何处理交互式命令:


[root@shell ~]# echo "redhat" | passwd --stdin tom &> /dev/null 


[root@shell ~]# echo -e "n\np\n1\n\n+500M\nw\n" | fdisk /dev/vdb &> /dev/null 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章