简体   繁体   English

目录命令行参数

[英]Directory command line argument

I'm having trouble taking in a path where to run the script as a command line argument, test is it exists, then changing to that path to perform work. 我在将脚本作为命令行参数运行的路径上遇到麻烦,请测试它是否存在,然后更改为该路径以执行工作。 Here what I'm trying: 这是我正在尝试的:

#!/bin/bash

scriptpath=$1

if [ $# -lt 1 ]
then
    echo "Usage: script.sh <directory_name>"
fi

if [ -d scriptpath ]
then
    # work......
else 
    echo "Directory does not exist"

fi

Change this: 更改此:

if [ -d scriptpath ]

to this: 对此:

if [ -d $scriptpath ]

Also, I recommend making use of "" , so that your script still behaves properly when the argument contains weird characters. 另外,我建议使用"" ,以便当参数包含奇怪的字符时,脚本仍然可以正常运行。 (Unix allows spaces, newlines, asterisks, even control characters inside filenames.) So: (Unix允许在文件名内使用空格,换行符,星号甚至控制字符。)

scriptpath="$1"

...

if [ -d "$scriptpath" ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM