我是 unix 新手,正在练习一个简单的脚本来解压缩指定目录中的大量文件。解压完成后,我需要程序将压缩文件移动到另一个文件夹中(我称之为旧压缩文件夹)。为简单起见,我删除了解压缩文件的代码部分,并且当前让程序适用于特定文件而不是 *tar.7z 文件扩展名。由于某种原因,mv 语句不起作用。当我尝试运行脚本时,Unix 会说以下内容。有人可以帮我解决这个问题吗?再说一次,我知道这是一个漫长的做事方式,但我想练习编写脚本。请友善一点,因为我对 Unix 很陌生:(
unzip5:第 14 行:[ASDE0002.tar.7z]:找不到命令
#!~/bin/bash
# My program to try to unzip several files with ending of tar.7z
# I have inserted the ability to enter the directory where you want this to be done
echo "What file location is required for unzipping?"
read dirloc
cd $dirloc
mkdir oldzippedfiles
for directory in $dirloc
do
if
[ASDE0002.tar.7z]
then
mv -f ASDE0002.tar.7z $dirloc/oldzippedfiles
fi
done
echo "unzipping of file is complete"
exit 0
最佳答案
[
是接受参数的(有时是内置的)命令的名称。因此,您需要在其后面添加一个空格,就像调用任何其他程序时一样。此外,您还需要进行测试。例如,判断文件是否存在并且是文件,需要使用-f
:
if [ -f ASDE0002.tar.7z ]
then
mv -f ASDE0002.tar.7z $dirloc/oldzippedfiles
fi
这里有一些other possible tests .
关于shell "if"语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14262715/