当前位置:首页 > 操作系统 > Linux

Linux Shell 脚本入门

linux shell 脚本格式
#!/bin/sh
#..... (注释)
命令...
命令...
使用vi 创建完成之后需设置权限 chmod +x filename.sh
执行命令: ./filename.sh
shell 中的局部变量
变量的声明,赋值,无需 $ 符号 , 如:
myUrl="abc"
myVar="asd"
需要注意等号两边不能有空格。
输出变量值
echo "myurl:${myUrl}"
echo "myvar:${myVar}"
通常调用shell 脚本时,外部需要传参,可通过 $[1...n] 接收外部传入参数如:
echo $1
echo "参数2:${2}"
从键盘接收参数:
echo "please input test:"
read txt
echo ${txt}
遍历当前目录输出文件名:
for filename in ls *.jpg
do
    echo $filename
done
for filename in ls -l
do
    ls -l $filename | awk ‘{print $9"t"$5}‘
done

判断目录或文件
for filename in ./*
do
    if test -f $filename
    then
        echo $filename is file
    fi
    if test -d $filename
    then
        echo $filename is mulu
    fi
done

遍历目录,输出目录中所有文件:
function read_dir(){
    for file in `ls $1`
    do
        if test -d $1"/"$file
        then
            read_dir $1"/"$file
        elif test -f $1"/"$file
        then
            echo $1"/"$file
        else
            echo "none"
        fi
    done
}

echo "please input mulu:"
read txt
read_dir $txt

原文:http://www.cnblogs.com/a_bu/p/4686603.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!