仅当文件未设置为可执行文件时,才需要在当前目录中运行命令

时间:2013-01-30 16:48:56

标签: linux bash shell tar

问题在于:

使用bash for循环,循环遍历当前目录中包含字符串“osl-guest”和“.tar.gz”的文件(使用'ls'命令,请参阅下面的示例输出),然后运行命令每个文件上的“tar -zxf”仅在文件未设置为可执行文件时单独使用。例如,要对文件'file1'运行'tar -zxf'命令,命令为:tar -zxf file1

“ls -l”的示例输出:

-rw-r--r--   1 lance lance   42866 Nov  1  2011 vmlinuz-2.6.35-gentoo-r9-osl-guest_i686.tar.gz
-rwxr-xr-x   1 lance lance   42866 Nov  1  2011 vmlinuz-3.4.5-gentoo-r3-osl-guest_i686.tar.gz
-rw-r--r--   1 lance lance   42866 Nov  1  2011 vmlinuz-3.5.3-gentoo-r2-osl-guest_i686.tar.gz

1 个答案:

答案 0 :(得分:1)

您可以通过以下方式执行循环,而无需调用ls

# For each file matching the pattern
for f in *osl-guest*.tar.gz; do
    # If the file is not executable
    if [[ ! -x "$f" ]]; then 
            tar -zxf $f;
    fi; 
done;

*osl-guest*.tar.gz只是使用shell扩展来获取所需的文件列表,而不是调用它ls

if语句检查文件是否可执行,-x是对可执行文件的测试,使用!否定结果,因此它只会输入{{1}当文件不可执行时阻止。