shell小脚本工具合集

1.将指定内容写入文件

echo "hello world" > file.txt
echo "hello world" >> file.txt //上面那个是覆盖,这个是追加

2.读取文件内容

写法一:
----------------------------------------------------------------------------
#!/bin/bash

while read line
do
    echo $line
done < file(待读取的文件)
----------------------------------------------------------------------------

写法二:
----------------------------------------------------------------------------
#!/bin/bash

cat file(待读取的文件) | while read line
do
    echo $line
done
----------------------------------------------------------------------------

写法三:
----------------------------------------------------------------------------
for line in `cat file(待读取的文件)`
do
    echo $line
done
----------------------------------------------------------------------------

说明:
for逐行读和while逐行读是有区别的,如:
$ cat file
aaaa
bbbb
cccc dddd

$ cat file | while read line; do echo $line; done
aaaa
bbbb
cccc dddd

$ for line in $(<file); do echo $line; done
aaaa
bbbb
cccc
dddd

 3.杀死某个程序的所有进程,grep后面添加杀死的条件

kill -9 `ps -ef | grep process_name | grep -v "grep" | awk '{print $2}'`

 

posted @ 2016-12-06 10:47  但行好事-莫问前程  阅读(241)  评论(0编辑  收藏  举报