shell脚本

编写脚本

1.xcall.sh

为了在任何目录下都可以执行,可以配环境变量或环境变量指定的目录,如:/usr/local/bin/
因为访问其他主机时,只能调用 在 /usr/bin 或 /usr/local/bin下配置的命令

2.xsync.sh

    #!/bin/bash
    if [[ $# -lt 1 ]] ; then echo no parameters; exit ; fi
    p=$1
    echo p=$p
    dir=`dirname $p `
    echo dir = $dir
    filename=`basename $p `  //注意前面反引号必须无空格;
    echo filename=$filename
    cd $dir
    fullpath=`pwd -P `
    echo fullpath=$fullpath
    user=`whoami`
    for (( i = 202 ; i <= 204 ; i = $i + 1 )) ; do
       echo ======= s$i =======
       rsync -lr $p ${user}@s$i:$fullpath
    done ;

  -l :软连接的复制
    xsync.sh /home/etc/a.txt
    rsync -lr /home/etc/a.txt centos@s202:/home/etc

-l:表示可以复制软连接(包括内部东西)
r:递归

 $?  //命令的返回值存储变量,0:成功 1:失败。
 $#  //参数个数
 $1  //第几个参数
 $0  //当前脚本(命令)名称
 $@  //取出所有参数
 shift //参数左移
 ${a/b/c} //


while循环实现99乘法表.

#!/bin/bash
i=1
lines=$1
while(( i<=lines )); do
j=1
    while(( j<=$i )) ;do
    echo -ne ${j}x${i}=$(( j*i ))'\t';              //-n不换行,-e:转义符,转义后面的 \t $(())是进行数值运算
    j=$(( j+1 ));
    done;
i=$(( i+1 ))
echo ;
done ;

xxcall.sh:(不使用for循环遍历数据节点的情况)
 1 #!/bin/bash
 2 
 3 params=$@
 4 
 5 hosts=`cat /soft/hadoop/etc/hadoop/slaves`
 6 
 7 for host in $hosts ; do
 8     tput setaf 2
 9     echo ============= $host =============
10     tput setaf 7
11     ssh -4 $host "source /etc/profile ; ${params}" 
12 done

 

posted on 2017-05-07 23:30  艺海浮台  阅读(192)  评论(0编辑  收藏  举报