程序編號 1(出現錯誤)
#!/bin/bash
echo "Enter source and destination directories: "
read $src $dest
if [ -d $src ] && [ -d $dest ]
then
echo "Process will start "
else
echo "Enter valid directories"
exit 1
fi
cp -r $src $dest
status=$?
if [ $? -eq 0 ]
then
echo "Successfully completed "
else
echo "facing some problems "
fi
第二個程序(執行時沒有出現任何錯誤)
#!/bin/bash
echo "Enter Sourse Directory name : "
read src
echo "Enter destination directory: "
read dest
if [ ! -d $src ]
then
echo "Enter Valid Directory name "
exit 1
elif [ ! -d $dest ]
then
echo "Enter Valid Destination source name "
exit 2
fi
cp -r $src $dest
status=$?
if [ $status -eq 0 ]
then
echo "File copied succesfully"
else
echo "there is a problem"
fi
答案1
一個巨大的差異就在你的眼前:
方案一:
read $src $dest
方案2:
read src
[...]
read dst
read
是一個 shell 內建函數(參見線上說明頁)提供的變數名稱必須不是首先$