shell脚本
shell脚本中的$*,$@和$#
举例说: 脚本名称叫test.sh 入参三个: 1 2 3 运行test.sh 1 2 3后 $*为"1 2 3"(一起被引号包住) $@为"1" "2" "3"(分别被包住) $#为3(参数数量)
shell获取当前目录名
1.basename
basename `pwd`
2.echo
You can use parameter substitution with the ${var##pattern} syntax, which removes from $var the longest part of $Pattern that matches the front end of $var. Take a look at an example:
echo ${PWD##*/}
3.awk
A more elaborate solution uses a combination of awk (a pattern-scanning utility) and rev (a utility that reverses lines from a file or from stdin):
cd /usr/share/cups/data
pwd | rev | awk –F \/ '{print $1}' | rev
data
It's a lot easier to understand this kind of script step by step:
pwd
/usr/share/cups/data
pwd | rev
atad/supc/erahs/rsu/
pwd | rev | awk –F \/ '{print $1}'
atad
pwd | rev | awk –F \/ '{print $1}' | rev
data
The -F option indicates that you should separate by fields, where the field delimiter is /, and that you should print field 1.
4.sed
Finally, you can parse pwd output in the stream editor sed using an elaborate regular expression. This approach may be educational, but it's not practical:
cd /home/smith/music
pwd | sed 's,^\(.*/\)\?\([^/]*\),\2,'
music
For a better understanding of how this works, remove the escape character (\), which is required for special characters such as "(":
sed 's,^(.*/)?([^/]*),\2,'