重定向
- ">" 符号:把 ls 的标准“1”输出,重定向到 lsoutput.txt 文件
ls -1 > lsoutput.txt
- ">>" 符号,把 ls 的标准“1”输出,添加到 lsoutput.txt 文件的尾部
ls -1 >> lsoutput.txt
- 文件标识符: 0 - 标准输入; 1 - 标准输出; 2 - 标准错误输出
重定向会先执行
cat mydata.txt | sort | uniq > mydata.txt
- 先执行了 > mydata.txt ,会立即清空文件(将文件截断为 0 字节)
编写shell脚本
#!/bin/sh
# first
# This file looks through all the files in the current
# directory for the string POSIX, and then prints the names of
# those files to the standard output.
for file in *
do
if grep -g POSIX $file
then
echo $file
done
exit 0
脚本执行
chmod +x first
./first # 最安全的执行方法