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
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
hello jack,welcome to my program