Variable scope in subshell

Posted by 4Aiur on 03/10/2012 in Shell |

Variable scope in subshell functions [mlf4aiur@4aiur Shell]$ cat function_return.sh subshell () { # pipeline will generate subshell echo 1 2 3 | while read line do echo “subshell: inside 1”; var=1; return 1 done echo “subshell: inside 0”; var=0; return 0 } subshell echo “subshell: outside return $? var=$var” foo () { for line in […]

Tags:

修改gb2312网页编码为utf-8

Posted by 4Aiur on 01/10/2011 in Shell |

修改gb2312网页编码为utf-8 find html/ -type f -name “*.html” | \ while read line do mkdir -p foo/dirname $line/ iconv -f GB18030 -t UTF-8 $line 2>/dev/null | \ sed /charset=gb2312/’s/charset=gb2312/charset=utf-8/’ \ > foo/$line done

Tags:

不需要反引号的运行ssh复杂远程命令方式

Posted by 4Aiur on 06/07/2010 in Shell |

不需要反引号的运行ssh复杂远程命令方式 之前当我需要执行复杂的运程命令时,总是要先处理好引号、变量等问题,之后再执行命令写起来太麻烦,今天在commandlinefu上学了一招非常棒的方法,记录一下。 举个例子,之前需要用\反引$符号 [root@localhost ~]# ssh 127.0.0.1 -l root “echo a b c | awk ‘{print \$2}'” b 现在我们可以把复杂的命令写到文件中执行。 [root@localhost ~]# cat cmd echo a b c | awk ‘{print $2}’ 方法1: [root@localhost ~]# ssh 127.0.0.1 -l root “$( 方法2: [root@localhost ~]# ssh 127.0.0.1 -l root “cat cmd” b 方法3,使用标准输入执行,输入完毕后使用ctrl-D提交命令: [root@localhost ~]# ssh 127.0.0.1 -l […]

Tags: ,

shell版按任意键继续

Posted by 4Aiur on 04/14/2010 in Shell |

按任意键继续的shell写法 read -sn1 -p “Press any key to continue…”; echo

Tags:

wget中文帮助

Posted by 4Aiur on 04/06/2010 in Shell |

wget help [mlf4aiur@4aiur ~]$ wget –help GNU Wget 1.13.4,非交互式的网络文件下载工具。 用法: wget [选项]… [URL]… 长选项所必须的参数在使用短选项时也是必须的。 启动: -V, –version 显示 Wget 的版本信息并退出。 -h, –help 打印此帮助。 -b, –background 启动后转入后台。 -e, –execute=COMMAND 运行一个“.wgetrc”风格的命令。 日志和输入文件: -o, –output-file=FILE 将日志信息写入 FILE。 -a, –append-output=FILE 将信息添加至 FILE。 -d, –debug 打印大量调试信息。 -q, –quiet 安静模式 (无信息输出)。 -v, –verbose 详尽的输出 (此为默认值)。 -nv, –no-verbose 关闭详尽输出,但不进入安静模式。 -i, –input-file=FILE 下载本地或外部 […]

Tags:

转换txt文件的字符编码

Posted by 4Aiur on 04/06/2010 in Shell |

转换txt文件的字符编码 find . -type f -name “*.txt” |\ while read line do if iconv -f GB2312 -t UTF-8 “$line” > /tmp/foo; then mv /tmp/foo “$line” else : fi done

Tags:

利用xargs实现多任务并发

Posted by 4Aiur on 04/06/2010 in Shell |

利用xargs实现多任务并发 [4aiur@localhost Temp]$ echo 127.0.0.1 192.168.2.50 192.168.10.108 | xargs -n1 -P2 -I % ping -c 3 % PING 127.0.0.1 (127.0.0.1): 56 data bytes 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.048 ms PING 192.168.2.50 (192.168.2.50): 56 data bytes 64 bytes from 192.168.2.50: icmp_seq=0 ttl=64 time=0.026 ms 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.091 ms 64 bytes […]

Tags:

目录名大小写转换

Posted by 4Aiur on 04/06/2010 in Shell |

使用find把目录名修改为大写——- find . -type d | sort -r |\ while read name do echo “mv $name ${name%/*}/echo ${name##*/} | tr '[:lower:]' '[:upper:]'” mv $name ${name%/*}/echo ${name##*/} | tr '[:lower:]' '[:upper:]' done 使用递归方式把目录名修改为小写 #!/bin/bash # set -x tolower () { ls | while read name do if [ -d $name ] ; then new_name=echo $name | […]

Tags:

摘自www.commandlinefu.com的一些命令

Posted by 4Aiur on 04/06/2010 in Shell |

摘自www.commandlinefu.com的一些命令 # change to the previous working directory $ cd – # quickly backup or copy a file with bash $ cp filename{,.bak} # mtr, better than traceroute and ping combined # mtr combines the functionality of the traceroute and ping programs in a single network diagnostic tool. # As mtr starts, it investigates the […]

Tags:

避免相同的程序重复运行

Posted by 4Aiur on 04/06/2010 in Shell |

避免相同的程序重复运行的小函数 逻辑如下: 检查pid文件是否存在,不存在则直接运行程序 如果pid文件存在,则检查进程中是否存在这个pid 进程存在此pid则退出 进程中不存在这一pid,则删除pid文件,程序继续运行 缺点:没有对进程名称进行对比 # Initialize variables app_path=”$(dirname $0)” pid=”${app_path}/run.pid” # Function TestPID () { if [ -f ${pid} ] ; then if ps -p cat ${pid} >/dev/null ; then echo “basename $0 is running! main process id = $(cat ${pid})” ps -p cat ${pid} -o cmd= exit 1 else rm -f […]

Tags:

Copyright © 2010-2024 4Aiur All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.5, from BuyNowShop.com.