![下面两个将内容从一个目录复制到另一个目录的程序有什么区别?](https://linux22.com/image/168906/%E4%B8%8B%E9%9D%A2%E4%B8%A4%E4%B8%AA%E5%B0%86%E5%86%85%E5%AE%B9%E4%BB%8E%E4%B8%80%E4%B8%AA%E7%9B%AE%E5%BD%95%E5%A4%8D%E5%88%B6%E5%88%B0%E5%8F%A6%E4%B8%80%E4%B8%AA%E7%9B%AE%E5%BD%95%E7%9A%84%E7%A8%8B%E5%BA%8F%E6%9C%89%E4%BB%80%E4%B9%88%E5%8C%BA%E5%88%AB%EF%BC%9F%20.png)
程序编号 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 内置函数(参见联机帮助页)提供的变量名称必须不是首先$