shell脚本
robberboyboy
这个作者很懒,什么都没留下…
展开
-
time 命令重定向问题
问题,一些命令输出无法定向到文件root@cloud01:~# time > a.txtreal 0m0.000suser 0m0.000ssys 0m0.000s执行后没有将输出打印到a.txt文件 time是shell关键字,将time 后面一整行作为命令执行了,root@cloud01:~# time ls > a.txtre原创 2012-11-02 15:12:42 · 612 阅读 · 0 评论 -
逐行读取文本内容 循环读取字符串内容
time.txt 文本文件#!/bin/bashx=0cat time.txt | while read linedo echo $x $line x=$(($x+1))done或者#!/bin/bashx=0while read linedo echo $x $line x=$(($x+1))done原创 2012-10-24 16:43:52 · 2114 阅读 · 0 评论 -
grep 多条件搜索
grep -e (条件)-e (条件) 或的关系 例如搜索文本中,包含ac 或者bc的行grep -e ’ac‘ -e ’bc‘原创 2012-12-18 16:49:24 · 645 阅读 · 0 评论 -
shell取系统时间做浮点运算,练习cut sed cat uniq
#!/bin/bashstart=$(date +%s.%N)ls >/dev/null 2>&1end=$(date +%s.%N)#取小数点后9位diff=$(echo "scale=9;($end - $start)"|bc)echo $diff#bc运算小于1的数,小数点前没有零,判断并补零if [ "$(echo $diff | cut -c原创 2012-10-24 13:57:38 · 1132 阅读 · 0 评论 -
shell中的指针-变量的间接引用
#!/bin/basha=abcdabcd=1234echo "a = $a"eval a=\$$aecho "now a = $a"# ./test.sh a = abcdnow a = 1234原创 2012-12-03 18:44:42 · 3420 阅读 · 0 评论 -
参数替换
${parameter-default} -- 如果变量parameter没被声明, 那么就使用默认值.${parameter:-default} -- 如果变量parameter没被设置, 那么就使用默认值.#!/bin/bashecho ${username-`whoami`}username=echo ${username:-`whoami`}username=inp原创 2012-12-03 16:32:42 · 386 阅读 · 0 评论 -
shell 函数,去掉传递进来参数开头的0
#!/bin/bash# 去掉传递进来参数开头的0(也可能是多个0),如果不去掉bash会把这个值当做8进制来解释strip_leading_zero (){ shopt -s extglob # 打开扩展通配(globbing) local val=${1##+(0)} # 使用局部变量,匹配最长连续一个或多个0原创 2012-12-01 10:20:30 · 4974 阅读 · 0 评论 -
shell条件判断 条件为何值时判断语句为真
#!/bin/bashif [ 0 ]then echo "0 is true"else echo "0 is flase"fiif [ 1 ]then echo "1 is true"else echo "1 is flase"fiif [ -1 ]then echo "-1 is true"原创 2012-11-30 14:00:10 · 1761 阅读 · 0 评论 -
单引号内引入变量 shell 命令输出赋值给变量,以及用变量替换命令
单引号内嵌套单引号即可使用变量。#!/bin/bashi=10echo $iecho '$i'echo '$i is : '$i''执行结果# ./test.sh 10$i$i is : 10单引号内嵌套单引号即可使用变量。将命令赋值给变量,和将命令输出内容赋值给变量#!/bin/bash: ${username=`who原创 2012-11-29 16:09:58 · 45106 阅读 · 3 评论 -
tar 备份文件
将当前文件夹下文件备份至/home 如果不输入备份文件名称则使用backup+date#!/bin/bashBACKFILE=back-$(date +%m-%d-%y) #备份文件中嵌入时间archive=${1:-$BACKFILE} #如果命令行中没有指定备份文件的文件名,将默认使用backupfiletar cvf - /usertest > /ho原创 2012-11-30 09:20:57 · 551 阅读 · 0 评论 -
监控linux 网络流量
修改了下网上搜索的监控网络流量的脚本 #!/bin/bash if [ -n "$1" ]; then eth_name=$1 else eth_name="eth0" fi i=0 send_o=`expr $(ifconfig $eth_name | grep bytes | awk '{print $原创 2012-11-16 11:48:44 · 503 阅读 · 0 评论 -
监控linux机器负载脚本
top.sh &每隔一分钟记录一次cpu负载情况到top.txt文件#!/bin/bashwhile [ 1 ]do top -b -n 1 |awk '/^top/||/Cpu/ {print}' >>top.txt sleep 1mdonetop - 17:53:48 up 1 day, 3:44, 2 users,原创 2012-11-14 17:55:16 · 434 阅读 · 0 评论 -
find 命令用法,根据时间,文件权限,查找文件
find pathname -options [-print -exec -ok]pathname find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。-print find命令将匹配的文件输出到标准输出。-exec find命令对匹配的文件执行该参数所给出的 s h e l l命令。相应命令的形式为 ' command' {} \;,注意{ }和\转载 2012-10-25 13:49:41 · 1666 阅读 · 0 评论 -
exec 重定向
num=$1exec 3>&1 #将标准输入定向到FD3 ,这样就不会子在屏幕打印exec 1>execlog.txt #将标准输入定向到文件日志execlog.txtif [ "$num" -eq 0 ];then echo "num 1 is 0" # 该信息在屏幕将不会显示fiexec原创 2015-01-26 11:06:10 · 972 阅读 · 0 评论