程序编号 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 内置函数(参见联机帮助页)提供的变量名称必须不是首先$