小编Don*_*Don的帖子

rm 在命令行上工作,但不在脚本中

当我rm *.old.*在命令行上执行它时,它会正确删除,但是当我在脚本的以下部分执行它时,它并没有 rm 所有*.old.*文件。

我的 bash 脚本有什么问题:

 for i in ./*; do
    if [[ -f $i ]];  then

        if [[ $i  ==  *.old.* ]]; then
                oldfile=$i
                echo "this file is to be removed: $oldfile"
                rm $oldfile
                exec 2>errorfile
            if [ -s $errorfile ]
            then
                echo "rm failed"
            else
                echo "removed $oldfile!"
            fi
        else
            echo "file with old extension  does not exist"
        fi

        orig=$i
        dest=$i.old
        cp $orig $dest
        echo "Copied $i"

    else
        echo "${i} is not a …
Run Code Online (Sandbox Code Playgroud)

bash scripts rm

13
推荐指数
3
解决办法
1万
查看次数

脚本中的cp错误

运行以下脚本时出现以下错误:

cp: cannot stat ls
Run Code Online (Sandbox Code Playgroud)

脚本内容

#!/bin/bash
#make copies of all files in directory
cd /home/don
LIST='ls'
for i in $LIST; do
    ORIG=$i
    DEST=$i.old
    cp $ORIG $DEST
    echo "Copied $i"
done
Run Code Online (Sandbox Code Playgroud)

有人可以看到问题吗?

command-line bash scripts

4
推荐指数
1
解决办法
1056
查看次数

标签 统计

bash ×2

scripts ×2

command-line ×1

rm ×1