Sunday, 15 February 2015

Bash + check for file exist with a path home ~ -



Bash + check for file exist with a path home ~ -

i haven't found deal particular situation. maybe there easy way i'm overlooking instead of checking string grab scenario. when check input existence of file, if input ~/filecheck , won't work. negative results while file in home folder. suggestions improvement part of script appreciate. have utilize input instead of argument. help.

my test script

read -p "enter: " input echo $input if [ -f $input ]; read -p "do want delete file?:" input2 if [[ $input2='y' || $input2 = 'y' ]] rm -f $input elif [[ $input2='n' || $input2='n' ]] exit else echo "invaild option" exit fi else echo invaild option! exit fi

since entering input string ~/filecheck shell doesn't expand tilde while using status -f in [ -f $input ]

you can utilize way but not recommended , potentially unsafe arbitrary commands can run user:

if [[ -f $(bash -c "echo $input") ]]; echo "file exists" fi

edit: per comments below avoid risky bash -c can use:

if [[ -f "${input/\~/$home}" ]]; echo "file exists" fi

bash file

No comments:

Post a Comment