shell "if"语句

标签 shell unix if-statement mv

我是 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/

相关文章:

linux - 如何查找哪些文件来自哪台机器?

linux - IF语句多个and or条件

c - 如何使用 C 程序中的 exec 命令更改目录?

Linux-wget 命令

linux - Unix 和 Linux 上应用程序/脚本缓存文件的正确位置是什么?

java - Java 处理的 2GB 文件

linux - 避免杀死启动脚本的进程的 Shell 脚本

PHP 处理 IF 语句中的多个子句

python - 如果输入 = 列表 [n] : How would I set this up?

java - 如何从字符串生成Java源代码?