第一次问栈溢出的问题。我是一名学生,正在开发一个小型 Linux 待办事项列表应用程序,用户可以在其中使用如下命令创建新文件:
./todo add "hello world"
...结果应该是待办事项列表中的新文本文件,其中包含文本“hello world”(或“”中的任何内容)。
到目前为止我所拥有的:我的 bash 脚本 add.sh 创建一个新文件,将其添加到待办事项列表并重命名该列表中的所有项目以创建一个编号列表,如下所示:
1.txt 2.txt 3.txt
这是我的 add.sh 代码:
echo $2 >> newFile.txt. #creates a new file
mv newFile.txt ~/todoApp/usrMenu/todo #moves file to todo list
cd ~/todoApp/usrMenu/add && ./name.sh #renumbers all files in the todo list
但这是我的问题: 如何让命令行(“hello world”)中的 $2 参数成为新文件中的文本?
现在,当我 cat 新文件时,它是空白的。我想要的是新文件显示为“hello world”。
提前致谢!
最佳答案
问题出在您的 ./todo
脚本中:
if [ $1 == "add" ]
then
./usrMenu/add/add.sh #calls add.sh
fi
您没有将参数从./todo
传递到add.sh
,所以$2
在add.sh
将为空,与 todo
中的 $2
不同。要传递所有参数,您可以使用"$@"
:
if [ "$1" == "add" ]
then
./usrMenu/add/add.sh "$@" #calls add.sh and forwards all parameters
fi
或者,只传递您关心的参数,然后在脚本中将变为 "$1"
(不是 "$2"):
./usrMenu/add/add.sh "$2" # Let "$2" in this script become "$1" in add.sh
关于linux - 将命令行参数打印到新的 Linux tex 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58758624/