您的位置:首页 > 运维架构 > Shell

shell脚本常用命令

2017-09-18 17:05 555 查看
basename
1. basename 是去除目录后剩下的名字
example:
shell> temp=/home/temp/1.test
shell> base=`basename $temp`
shell> echo $base
结果为:1.test
2. dirname 是取目录
example:
shell> temp=/home/temp/1.test
shell> dir=`dirname $temp`
shell> echo $dir
结果为:/home/temp

另一种实现的方法:
${var##*/}   把变量var最后一个/以及左边的内容去掉

shell> var=/home/temp/1.test
shell> echo ${var##*/}
结果为:1.test

${var%/*}  把变量var最后一个/以及右边的内容去掉

shell> var=/home/temp/1.test
shell> echo ${var%/*}
结果为:/home/temp

3.read命令
创建shell脚本:test.sh
内容如下:

#!/bin/bash
echo -n "Enter your name:"   //参数-n的作用是不换行,echo默认是换行
read  name                 
echo "hello $name,welcome to my program"      
exit 0                                   //退出shell程序。
执行脚本 sh test.sh 结果如下:

Enter your name:jack

hello jack,welcome to my program

//********************************
由于read命令提供了-p参数,允许在read命令行中直接指定一个提示。
所以上面的脚本可以简写成下面的脚本::
#!/bin/bash
read -p "Enter your name:" name
echo "hello $name, welcome to my program"
exit 0

Enter your name:jack

hello jack,welcome to my program
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: