这是我下面的bash脚本
cat >> $file_name
我收到这种错误:
./l7.sh:第 12 行:$file_名称:不明确的重定向
以下是完整代码
https://github.com/vats147/public/blob/main/l7.sh
为什么我会收到此错误? 甚至我的语法也是正确的。
在参数file_name
中,您必须分配$1
,这将作为输入参数传递给当前文件。
#! /bin/bash
echo -e " Enter file name : c"
read file_name=$1
if [ -f $file_name ]
then
if [ -w $file_name ]
then
echo " type some text data. to quit press enter "
#cat > $file_name(single angular bracket use for overwritten)
#cat >> $file_name(two angular bracket use for appending a text)
cat >> $file_name
else
echo " file not have write permission"
fi
else
echo "file not exist"
fi
这些是脚本的位置参数。
执行./script.sh Hello World
将使
$0 = ./script.sh
$1 = Hello
$2 = World
注意
如果你执行./script.sh
,$0
会给出输出./script.sh
但如果用 bashscript.sh
执行它,它会给出输出script.sh
。