Shell 重定向到从命令行获取的文件
我是初学者 我试图从shell中的提示符中询问文件名 然后在另一个shell中编辑该文件,如下所示:Shell 重定向到从命令行获取的文件,shell,Shell,我是初学者 我试图从shell中的提示符中询问文件名 然后在另一个shell中编辑该文件,如下所示: test.sh echo "enter file name" read word sh test2.sh read number echo "$number" >> $word test2.sh echo "enter file name" read word sh test2.sh read number echo "$number" >> $word 我犯了一
test.sh
echo "enter file name"
read word
sh test2.sh
read number
echo "$number" >> $word
test2.sh
echo "enter file name"
read word
sh test2.sh
read number
echo "$number" >> $word
我犯了一个错误
Test2.sh: line 1: $mAmbiguous redirect
有什么建议吗?如果希望
test.sh
中的变量对其子进程可见,则需要导出它。在您的情况下,您似乎想要导出word
。不过,test2.sh
接受目标文件作为参数可能是更好的方法
test.sh
echo "enter file name"
read word
test2.sh "$word"
test2.sh
#!/bin/sh
: ${1?must have destination file name} # fail if missing
read number
echo "$number" >> "$1"
您可能需要将其复制到
路径中,或者使用/test2.sh
。显然,该文件需要有执行权限(chmod+x./test2.sh
)。