`
weiqiang.yang
  • 浏览: 156540 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

shell programming sheat sheet for myself

 
阅读更多
语法总是记不牢,文字记录之,搞个cheat sheet,自己用。。

1. 数值计算
a=0; (( a = a +1 )); echo $a

2. for循环
for f in `ls`; do echo $f; done

3. while循环
a=0; while [ $a -lt 10 ]; do echo -n "$a "; (( a = a+1 )); sleep 1; done ; echo;
a=0; while (( a < 10 )); do echo -n "$a "; (( a = a +1 )); sleep 1; done ; echo;

4. if判断
if [ -d /usr/local/bin ] ; then echo "directory exists."; else echo "directory not exists."; fi
if test -d /usr/local/bin; then echo "directory exists."; else echo "directory not exists."; fi
[ -d /usr/local/bin ] && echo "directory exists"
[ -d /user/local/bin ] || echo "directory not exists"

# for integers
num1 -eq num2 True if num1 equals num2.
num1 -ne num2 True if num1 is not equal to num2.
num1 -lt num2 True if num1 is less than num2.
num1 -gt num2 True if num1 is greater than num2.
num1 -le num2 True if num1 is less than or equal to num2.
num1 -ge num2 True if num1 is greater than or equal to num2.

# for string
str1 = str2 True if str1 and str2 are identical.
str1 != str2 True if str1 and str2 are not identical.
-n str1 True if str1 is not null (length is greater than zero).
-z str1 True if str1 is null (length is zero).

# for file conditions
-f somefile True if somefile exists and is an ordinary file.
-d somefile True if somefile exists and is a directory.
-s somefile True if somefile contains data (the size is not zero).
-r somefile True if somefile is readable.
-w somefile True if somefile is writable.
-x somefile True if somefile is executable.

# logical operators
cond1 -a cond2 True if both cond1 and cond2 are true.
cond1 -o cond2 True if either cond1 or cond2 is true.
! cond1 True if cond1 is false.


5. sed
sed 's/one/two/g' < in.file.txt > out.file.txt
sed -r 's/[0-9]{3,}//g' < in.file.txt  <-- 注1
sed 's:old:new:g' < in.file.text > out.file.txt <-- 注2
sed -r 's/[0-9]{3,}/(&)/g' < sed.txt
sed -r 's/([0-9]+)\s+([a-z]+)/\2 \1/g' < sed.txt <-- 注3
sed -n -r '/\S+/p' < sed.txt <-- 注4
sed -r '/^$/d' < sed.txt
sed -n -r 's/([0-9]+)\s+([a-z]+)/\2 \1/gpw test.txt' < sed.txt <-- 注5
sed -r -e 's/([0-9]+)\s+([a-z]+)/\2 \1/g' -e '/^$/d' < sed.txt  <--注6

注1: -r参数启用extended regular expression,原来自带的太弱了,而且要记好几套正则表达式很烦,干脆只要用到正则表达式的地方都启用扩展的,比如grep -E "[0-9]+"
注2: 分割符号可以是 / , :  ,  _ , |
注3: 如果不启用-re那么'('还得写成'\(',很恶心
注4: sed默认会打印每一行(不论有没匹配),-n表示默认不打印,后面的p表示如果匹配了那执行打印该行,整行意思是打印非空的行
注5: /p 答应 /g 全局 /w file 结果写到文件,可以组合多个command
注6: 多条sed指令用-e分开
分享到:
评论
5 楼 weiqiang.yang 2012-02-12  
man expect出来的文档太长了,简直就是一篇长篇论文,看半天没看懂怎么用,最后还是搜example,ssh代理自动登录简易脚本(我只是需要简单的自动发送密码过去。。而已)
ssh的-C参数可以启用gzip压缩,默认压缩级别是6,理论上说,如果代理是用来传输文本数据的话,是能提高不少传输效率的,如果用来看视频,或者传输其他二进制文件,那还是不要启用比较好

#!/usr/bin/expect -f

set timeout 10
spawn ssh -C -D 7070 -Nv username@sshhost -p 22 \
    -o GSSAPIAuthentication=no \
    -o PasswordAuthentication=yes \
    -o PubkeyAuthentication=no
expect "password:"
send "thepassword\n"
interact
4 楼 weiqiang.yang 2012-02-08  
参数原来是这么处理的。。。
#!/bin/bash
SEC=0
MIN=0
HOUR=0
while getopts :s:m:h: INPUT 2>/dev/null; do
    case $INPUT in
      s) SEC=$OPTARG
        ;;
      m) MIN=$OPTARG
        ;;
      h) HOUR=$OPTARG
        ;;
      *) echo "usage $0 -s SEC -m MIN -h HOUR"
         exit 1
        ;;
    esac
done
echo "$HOUR:$MIN:$SEC"
3 楼 weiqiang.yang 2012-02-08  
#!/bin/bash

INPUT=$1
case $INPUT in
  start)
    echo "starting"
    ;;
  stop)
    echo "stopping"
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  *)
    echo "usage $0 {start|stop|restart}"
    ;;
esac

2 楼 weiqiang.yang 2012-02-08  
#!/bin/bash

# $#    Number of command line arguments. Useful to test no. of command line args in shell script.
# $*    All arguments to shell
# $@    Same as above
# $-    Option supplied to shell
# $$    PID of shell
# $!    PID of last started background process (started with &)

echo "doing a time consuming job ..."
while true ; do
    echo -n  " * " ; sleep 1
done 2>&1 &
PID=$!

# doing a system backup or something
sleep 5

kill $PID ; echo "done!"

原来shell下面的进度条大概是这么做的。。。
1 楼 weiqiang.yang 2012-02-08  
#!/bin/bash

# generate a random integer
MAGIC=$(( $RANDOM % 100 ))
LOWER=0
UPPER=100

while true; do
    echo -n "$LOWER to $UPPER: "
    read INPUT
    if [ $INPUT -lt $MAGIC ] ; then
        LOWER=$INPUT
    elif [ $INPUT -gt $MAGIC ] ; then
        UPPER=$INPUT
    else
        echo "yep, the magic is $MAGIC"
        break
    fi
done

相关推荐

Global site tag (gtag.js) - Google Analytics