从命令行解析shell脚本的参数

时间:2022-07-07 22:23:02

I am still a junior with the linux shell script and would like help with a certain script.

我还是linux shell脚本的大三学生,我想帮助您编写一个特定的脚本。

I would run a sample shell script such as the following from the command line that takes in a directory as an argument:

我将运行一个示例shell脚本,例如命令行中的以下脚本,该脚本接受一个目录作为参数:

./script.sh /some_dir/some_exe

How can I parse out the "some_dir" in my shell script?

如何解析shell脚本中的“some_dir”?

Thanks.

谢谢。

2 个解决方案

#1


3  

The dirname command extracts the directory name from a string; so

dirname命令从字符串中提取目录名;所以

THEDIR=`basename "$1"`

should do the trick.

应该足够了。

#2


0  

If you are using Bash, it should be stored in $1. I'm pretty sure it's the same for other shells.

如果您正在使用Bash,那么它应该存储在$1中。我很确定其他的壳层也是如此。

#1


3  

The dirname command extracts the directory name from a string; so

dirname命令从字符串中提取目录名;所以

THEDIR=`basename "$1"`

should do the trick.

应该足够了。

#2


0  

If you are using Bash, it should be stored in $1. I'm pretty sure it's the same for other shells.

如果您正在使用Bash,那么它应该存储在$1中。我很确定其他的壳层也是如此。