Linux Shell 编程学习实践(二)——认识Shell编程
read 可以从键盘或文件的某一行文本中读入信息,并将其赋给一个变量
junsheng@ubuntu:~$ cat readtest.sh
#!/bin/bash
#readname
echo -n "firstname"
read firstname
echo -n "lasrname"
read lastname
echo "this name is $firstname $lastname"
junsheng@ubuntu:~$ chmod u+x readtest.sh
junsheng@ubuntu:~$ sh readtest.sh
firstnamewang
lasrnamejunsheng
this name is wang junsheng
管道 |
tee 把输出的一个副本输送到标准输出,另一个副本拷贝到相应的文件中,一般与管道合用
junsheng@ubuntu:~$ echo "test" |tee mylogfile.txt
test
junsheng@ubuntu:~$ cat mylogfile.txt
test
junsheng@ubuntu:~$
文件重定向
command>filename
---覆盖输出
command>>filename
---追加输出
command>filename>&1
---把标准输出和标准错误重定向
command<<delimiter
---输入直到delimiter分解符
command<filename ----输入文件内容到命令
command<-
---
关闭标准输入
>nullfile.txt
---创建字节为0的文件
command1<filename>command3
---按从左到右顺序执行
eg
junsheng@ubuntu:~$ df -lh>myfile
junsheng@ubuntu:~$ cat myfile
文件系统 容量 已用 可用 已用%% 挂载点
/dev/loop0 15G 3.9G 9.7G 29% /
none 993M 280K 992M 1% /dev
none 998M 720K 998M 1% /dev/shm
none 998M 100K 998M 1% /var/run
none 998M 0 998M 0% /var/lock
/dev/sda1 49G 41G 7.9G 84% /host
/dev/sdb4 3.8G 2.5G 1.3G 67% /media/90C2-ACFF
junsheng@ubuntu:~$ df -lh>>myfile
junsheng@ubuntu:~$ cat myfile
文件系统 容量 已用 可用 已用%% 挂载点
/dev/loop0 15G 3.9G 9.7G 29% /
none 993M 280K 992M 1% /dev
none 998M 720K 998M 1% /dev/shm
none 998M 100K 998M 1% /var/run
none 998M 0 998M 0% /var/lock
/dev/sda1 49G 41G 7.9G 84% /host
/dev/sdb4 3.8G 2.5G 1.3G 67% /media/90C2-ACFF
文件系统 容量 已用 可用 已用%% 挂载点
/dev/loop0 15G 3.9G 9.7G 29% /
none 993M 280K 992M 1% /dev
none 998M 720K 998M 1% /dev/shm
none 998M 100K 998M 1% /var/run
none 998M 0 998M 0% /var/lock
/dev/sda1 49G 41G 7.9G 84% /host
/dev/sdb4 3.8G 2.5G 1.3G 67% /media/90C2-ACFF
junsheng@ubuntu:~$ cat >>myfile<<exit
> beijing
> shanxi
> exit
junsheng@ubuntu:~$ cat myfile
文件系统 容量 已用 可用 已用%% 挂载点
/dev/loop0 15G 3.9G 9.7G 29% /
none 993M 280K 992M 1% /dev
none 998M 720K 998M 1% /dev/shm
none 998M 100K 998M 1% /var/run
none 998M 0 998M 0% /var/lock
/dev/sda1 49G 41G 7.9G 84% /host
/dev/sdb4 3.8G 2.5G 1.3G 67% /media/90C2-ACFF
文件系统 容量 已用 可用 已用%% 挂载点
/dev/loop0 15G 3.9G 9.7G 29% /
none 993M 280K 992M 1% /dev
none 998M 720K 998M 1% /dev/shm
none 998M 100K 998M 1% /var/run
none 998M 0 998M 0% /var/lock
/dev/sda1 49G 41G 7.9G 84% /host
/dev/sdb4 3.8G 2.5G 1.3G 67% /media/90C2-ACFF
beijing
shanxi

浙公网安备 33010602011771号