[英]how to enter a directory as a command line argument
#!/bin/bash
echo Enter a Directory Name:
read
if [ -d "$1" ];
then
find "$1" -type f -size 0 -delete
find "$1" *.tmp -type f -delete
find "$1" *.swp -type f -delete
tar -cvzf mytarfile.tgz "$1"
else
echo "This is not a directory"
fi
The script runs when I put in the directory myself using ./program.sh Desktop. 当我自己使用./program.sh Desktop将目录放在目录中时,脚本将运行。 If I have an empty txt, .tmp and .swp file on my desktop it removes them and makes a tar file.
如果我的桌面上没有txt,.tmp和.swp文件,它将删除它们并生成tar文件。 How can I enter a directory at the (read) line for example Documents or MyMusic.
如何在(读取)行中输入目录,例如Documents或MyMusic。 It allows me to type in something but then goes right to the else and prints "This is not a directory.
它允许我键入某些内容,然后转到其他位置并打印“这不是目录。
Replace all $1
with $REPLY
. 将所有
$1
替换$1
$REPLY
。
Take a look at read's syntax: help read
看一下read的语法:
help read
You can use a variable to read the Directory name,like: 您可以使用变量来读取目录名称,例如:
echo "Enter a Directory Name:"
read Dir_Name
or better: 或更好:
read -p "Enter Directory Name:" Dir_Name
and then replace all $1
with ${Dir_Name}
然后将所有
$1
替换$1
${Dir_Name}
$1
is a special variable in Unix. $1
是Unix中的特殊变量。 Read it here: special variables 在这里阅读: 特殊变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.