简体   繁体   English

以文件名作为参数的shell脚本,并从Unix / Linux / Ubuntu中pwd上方的每个目录中删除此文件

[英]shell script that takes a file name as an argument and deletes this file from every directory above the pwd in Unix / Linux / Ubuntu

my shell script is as follows. 我的shell脚本如下。

if [ $# -eq 0 ]
then
    echo "pass the file name"
    exit
 fi
fl=$1
ch=1
h=/home

while [ $ch -eq 1 ]
do
    cd ..
    p=`pwd`
    echo "$p/$fl"
    rm `echo "$p/$fl"` 2> /dev/nullif [ $p = $h ]
    then
            ch=0
    fi
done

but when executing shows a syntax error. 但执行时显示语法错误。 line16: syntax error near unexpected token then' line 16: then' line16:意外令牌附近的语法错误then' line 16:然后'

What is the mistake in this script? 这个脚本有什么错误?

You misplaced if the script. 如果脚本你放错了地方。

if [ $# -eq 0 ]
then
    echo "pass the file name"
    exit
fi

fl=$1
ch=1
h=/home

while [ $ch -eq 1 ]
do
    cd ..
    p=`pwd`
    echo "$p/$fl"
    rm `echo "$p/$fl"` 2> /dev/null
    if [ $p = $h ]
    then
            ch=0
    fi
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM