1. 程式人生 > >Linux IO和管道練習題

Linux IO和管道練習題

l命令 信息 usr magedu write 不同 out hello pre

把/etc/fstab?件內容重定向到/tmp?錄下?件名為fstab.out

cat /etc/fstab > /tmp/fstab.out

把hello world追加到/tmp/fstab.out?件尾部

echo "hello world" >>/tmp/fstab.out

禁?覆蓋重定向和強制重定向

set -C
echo "hello magedu" >/tmp/fstab.out
-bash: /tmp/fstab.out: cannot overwrite existing file

設置禁?覆蓋重定向後,可強制覆蓋重定向

echo "hello magedu" >| /tmp/fstab.out
cat /tmp/fstab.out
hello magedu

把標準錯誤和標準輸出分別重覆蓋定向到不同的?件?,即標準錯誤重定向到falt.txt?件,標準輸出重定向到correct.txt

which cat 2> falt.txt > correct.txt
cat correct.txt
/usr/bin/cat
cat falt.txt
wih cat 2> falt.txt > correct.txt
cat falt.txt
cat correct.txt

合並標準輸出和標準錯誤覆蓋重定向到out.txt?件?

which cat &> out.txt
cat out.txt
/usr/bin/cat
 hich cat &>> out.txt
cat out.txt
/usr/bin/cat
bash: hich: command not found...

把所有?寫字?轉換為?寫

tr a-z A-Z </etc/issue

把out.txt?件?的內容,寫到file.txt?件?

cat >file.txt <out.txt

使?mail命令root給lsj普通?戶發郵件,要求郵件標題為”help”,郵件正?如下:

Hello, I am ?戶名,The system version is here,please help me to check it thanks!

操作系統版本信息

mail -s "help" lsj <<EOF
> Hello, I am `who`,The system version is here,please help me to check it thanks!
> `cat /etc/redhat-release`
> EOF

將/etc/issue?件中的內容轉換為?寫後保存?/tmp/issue.out?件中

cat /etc/issue|tr ‘a-z‘ ‘A-Z‘ > /tmp/issue.out

將當前系統登錄?戶的信息轉換為?寫後保存?/tmp/who.out?件中

 who |tr ‘[:lower:]‘ ‘[:upper:]‘ > /tmp/who.out
或者
who |tr ‘a-z‘ ‘A-Z‘ > /tmp/who.out

?個linux?戶給root發郵件,要求郵件標題為”help” ,郵件正?如下: Hello, I am ?戶名,The systemversion is here,please help me to check it ,thanks! 操作系統版本信息

[root@magedu ~]# mail -s help root << EOF
>Hello, I am `whoami`.
>The system version is here,please help me to check it,thanks!
>`cat /etc/redhat-release`
>EOF

將/root/下?件列表,顯?成??,並?件名之間?空格隔開

ls /root |tr ‘\n‘ ‘ ‘
或:
 ls -1 |tr ‘\n‘ ‘ ‘

計算1+2+3+..+99+100的總和

echo {1..100} | tr ‘ ‘ + | bc

刪除Windows?本?件中的?^M? 字符

cat test.txt |tr -d ‘\r‘ > newtest.txt

處理字符串“xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4”,只保留其中的數字和空格

echo "xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4" | tr -dc ‘[:digit:]
[:space:]‘
echo ‘xt.,l 1 jr#!$mn2 c*/fe3 uz4‘ |tr -d ‘[:punct:]‘ |tr -d ‘a-z‘

將PATH變量每個?錄顯?在獨?的??

echo $PATH |tr ‘:‘ ‘\n‘

將指定?件中0-9分別替代成a-j

echo {0..9}|tr ‘0-9‘ ‘a-j‘

將?件/etc/centos-release中每個單詞(由字?組成)顯?在獨???,並?空?

cat /etc/centos-release |tr -cs ‘[:alpha:]‘ ‘\n‘

Linux IO和管道練習題