我运行这段代码时遇到问题,有人有想法吗
#! /bin/bash
while :
do
echo "Enter file name along with absolute path : "
read -r inputFile
echo "Enter path you would like to save copy of the file : "
read -r pathinput
path=$pathinput
if ((-f "$inputFile" ; -d "$pathinput" ))
then
cp -p $inputFile $path
break
else
echo "File does not exist. Try again."
fi
done
echo " Job Done"
答案1
有两件事引起了我的注意:
if ((-f "$inputFile" ; -d "$pathinput" ))
似乎是 bash 算术和测试的混合。你想做的是:if [[ -f $inputFile && -d $pathinput ]]
cp -p $inputFile $path
VScp -p "$inputFile" "$path"
使用更多引号!
学习如何在shell中正确引用,这非常重要:
“双引号”包含空格/元字符的每个文字以及每一个扩张:
"$var"
,"$(command "$var")"
,"${array[@]}"
,"a & b"
。用于'single quotes'
代码或文字$'s: 'Costs $5 US'
,ssh host 'echo "$HOSTNAME"'
.看
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words
考虑使用https://www.shellcheck.net/乍一看,当您遇到 shell 问题时