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
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
相关文章推荐
- Shell 脚本常用命令
- 第10课shell脚本基础3-函数及常用命令
- Shell 脚本常用命令
- shell脚本常用批处理命令
- shell脚本基础与常用命令
- 常用Shell脚本命令
- 【shell脚本开发技术】linux中shell script脚本开发编程常用命令与脚本速记指南
- Linux新手生存笔记[10]——shell脚本基础3-函数及常用命令
- shell脚本基础——常用的sed命令举例
- linux学习笔记(20) - shell脚本编写常用判断命令
- Linux命令行与shell脚本(2)--常用命令
- shell脚本常用命令与方法总结
- shell脚本中常用的命令
- shell脚本常用命令
- Shell脚本常用unix命令
- Shell脚本中一些常用命令
- shell脚本常用命令简单总结
- shell常用的脚本处理命令
- shell脚本中常用的命令:wget、curl、ss、lsof、nmap、nc、netstat、telnet