- #Shell输入和输出
- #echo:显示文本行活变量,或者把字符串输入到文件,默认回车换行
- #\c:回车不换行
- #\f:禁止
- #\t:跳格,相当于按了一个Tab键
- #\n:回车换行
- #更名
- #sudo mv edhod.sh echod
- #改变权限
- #chmod 755 echod
- #cat echod
- #!/bin/bash
- #echod
- echo -e "This echo's 3 new lines\n\n\n"
- echo "ok"
- echo
- echo "This echo's 3 new lines\n\n\n"
- echo "The log files have all been done">mylogfile.txt
- #创mylogfile.txt文件
- sudo touch mylogfile.txt
- #执行(-e解析特殊字符)
- ./echod
- This echo's 3 new lines
- ok
- This echo's 3 new lines\n\n\n
- #演示回车换行和回车不换行
- [root@localhost 0330]# cat mylogfile.txt
- The log files have all been done
- #回车换行
- [root@localhost 0330]# echo "The log files have all been done" Th log files have all been done
- #回车不换行
- [root@localhost 0330]# echo -n "The log files have all been done"
- The log files have all been done[root@localhost 0330]#
- #read
- #read语句可以从键盘或者文件的某一行文本中读入信息,并将其赋给一个变量
- #vi readname
- chmod 755 readname
- [root@localhost 0330]# ./readname
- First Name:Chinaitlab
- Last Name:Shen zhen
- Your First Name is:Chinaitlab
- Your Last Name is:Shen zhen
- #演示以空格作为分隔符
- [root@localhost 0330]#
- cat readname
- #!/bin/bash
- #readname
- echo -n "First Name:"
- read firstname
- echo -n "Last Name:"
- read lastname subname
- echo -e "Your First Name is:${firstname}\n"
- echo -e "Your Last Name is:${lastname}\n"
- echo -e "Your Subname is:${subname}\n"
- [root@localhost 0330]# ./readname
- First Name:chinaitlab
- Last Name:shen zhen
- Your First Name is:chinaitlab
- Your Last Name is:shen
- Your Subname is:zhen
- #cat
- man cat
- #单独显示myfile内容
- cat myfile
- #显示myfile1 myfile2 myfile3的内容
- [root@localhost 0330]# cat myfile1 myfile2 myfile3
- this is myfile1
- this is myfile2
- this is myfile3
- #将myfile1 myfile2 myfile3重定向到myfile123
- [root@localhost 0330]# cat myfile1 myfile2 myfile3 > myfile123
- [root@localhost 0330]# cat myfile123
- this is myfile1
- this is myfile2
- this is myfile3
- #dos传输的文本
- cat -v dos.txt
- #不会分页显示
- ls -l /bin > ls.txt
- cat ls.txt
- #分页显示方法一
- cat ls.txt | more
- #分页显示方法二
- cat ls.txt | less
- #分页显示方法三
- less ls.txt
- #分页显示方法四
- more ls.tx
- #管道(|)
- #分页显示
- #cat ls.txt | more
- #查找myfile
- #ls -l | grep "myfile"
- #显示分区信息
- #df –k
- #打印第一列分区信息
- #df -k | awk '{print $1}'
- #过滤掉Filesystem
- #df -k | awk '{print $1}' | grep -v "Filesystem"
- #tee
- #把输出的一个副本输送到标准输出,另一个副本拷贝到相应的文件中
- [root@localhost 0330]# who | tee who.out
- root pts/0 Mar 30 19:00 (192.168.223.1)
- [root@localhost 0330]# who | tee who.out
- [root@localhost 0330]# cat who.out
- root pts/0 Mar 30 19:00 (192.168.223.1)
- [root@localhost 0330]# who | tee -a who.out
- root pts/0 Mar 30 19:00 (192.168.223.1)
- [root@localhost 0330]# cat who.out
- root pts/0 Mar 30 19:00 (192.168.223.1)
- root pts/0 Mar 30 19:00 (192.168.223.1)
- #将分区内容打印到屏幕并写入到文件
- [root@localhost 0330]# df -k | awk '{print $1}' | grep -v "Filesystem" | tee partation.txt
- /dev/sda2
- /dev/sda1
- none
- .host:/
- [root@localhost 0330]# cat partation.txt
- /dev/sda2
- /dev/sda1
- none
- .host:/
- #标准输入、输出和错误
- #标准输入 0 缺省键盘
- #标准输出 1 缺省屏幕
- #标准错误 2 缺省屏幕
- #文件重定向
- #> 重定向新文件
- #>> 追加
- [root@localhost 0330]# cat file
- jenny
- john
- wendy
- annie
- marry
- jack
- #将排序内容重定向到sort.out
- [root@localhost 0330]# cat file | sort 1>sort.out
- [root@localhost 0330]# cat sort.out
- annie
- jack
- jenny
- john
- marry
- wendy
- #cat file | sort >sort.out等价于cat file | sort 1>sort.out
- #演示追加
- [root@localhost 0330]# cat path.out
- /home/wgb/shell/0330
- [root@localhost 0330]# pwd>>path.out
- [root@localhost 0330]# cat path.out
- /home/wgb/shell/0330
- /home/wgb/shell/0330
- #巧妙创建文件
- [root@localhost 0330]# ls -al nullfile
- ls: nullfile: No such file or directory
- [root@localhost 0330]# >nullfile
- [root@localhost 0330]# ls -al nullfile
- -rw-r--r-- 1 root root 0 Mar 30 19:36 nullfile
- #
- [root@localhost 0330]# sort <file
- annie
- jack
- jenny
- john
- marry
- wendy
- [root@localhost 0330]# cat name.txt
- chinaitlab
- shenzhen
- #将name.txt的内容作为输入传递给sort并将排序结果输出到name.out
- [root@localhost 0330]# sort< name.txt >name.out
- [root@localhost 0330]# cat name.out
- chinaitlab
- shenzhen
- #终止符,输入某个输入终止输入
- [root@localhost 0330]# cat term.txt
- this is a test
- [root@localhost 0330]# cat >>term.txt <<CHINAITLAB
- > Hello,there I am using a $TERM terminal
- > and my username is $LOGNAME
- > BYE ...
- > CHINAITLAB
- [root@localhost 0330]# cat term.txt
- this is a test
- Hello,there I am using a xterm terminal
- and my username is root
- BYE ...
- #将错误内容重定向到文件
- [root@localhost 0330]# grep "trident" missiles
- grep: missiles: No such file or directory
- [root@localhost 0330]# ls -al missile 2> err_message.txt
- [root@localhost 0330]# cat err_message.txt
- ls: missile: No such file or directory
- #/dev/null:无底洞
- #将错误内容抛出
- [root@localhost 0330]# grep "trident" missiles 2>/dev/null
- cat /dev/null
- #显示account_new.txt account_old.txt的内容,将标准输入到accounts.out,错误输出到accounts_err
- [root@localhost 0330]# cat account_new.txt account_old.txt 1>accounts.out 2>accounts_err
- [root@localhost 0330]# cat account_new.txt
- account_old.txt 1>accounts.out 2>accounts.err
- [root@localhost 0330]# cat account_old.txt
- cat: account_old.txt: No such file or directory
- [root@localhost 0330]# cat accounts.out
- account_old.txt 1>accounts.out 2>accounts.err
- [root@localhost 0330]# ls -al account_old.txt
- ls: account_old.txt: No such file or directory
- [root@localhost 0330]# cat accounts_err
- cat: account_old.txt: No such file or directory
- [root@localhost 0330]# cat account.err
- cat: account.err: No such file or directory
- #合并标准输出和标准错误
- [root@localhost 0330]# grep "standard" standard.txt > grep.out 2>&1
- [root@localhost 0330]# cat grep.out
- grep: standard.txt: No such file or directory
- vi standard.txt
- [root@localhost 0330]# cat standard.txt
- china it lab
- shen zhen
- standard shell
- bash
- linux shell
- redhat
- [root@localhost 0330]# grep "standard" standard.txt > grep.out 2>&1
- [root@localhost 0330]# cat grep.out
- standard shell
- #exec
- exec ./helloworld.sh
- #重启Shell
- #重新登录并设置环境变量
- [root@localhost root]# export CHINAITLAB=ShenZhen
- [root@localhost root]# echo $CHINAITLAB
- ShenZhen
- #再次执行脚本,再次打印刚才设置的环境变量时无内容
- exec ./helloworld.sh
- echo $CHINAITLAB
- #文件描述符(exec和文件描述符结合使用不会重启Shell)
- [root@localhost 0330]# vi file_desc
- [root@localhost 0330]# cat file_desc
- #!/bin/bash
- #file_desc
- exec 3<&0 0<name.txt
- read line1
- read line2
- exec 0<&3
- echo $line1
- echo $line2
- [root@localhost 0330]# vi name.txt
- [root@localhost 0330]# chmod 755 file_desc
- [root@localhost 0330]# cat name.txt
- chinaitlab
- shenzhen
- [root@localhost 0330]# ./file_desc
- chinaitlab
- shenzhen
shell基础编程——输出和输入
最新推荐文章于 2024-02-04 15:42:07 发布