将命令行参数打印到新的Linux tex文件中

将命令行参数打印到新的Linux tex文件中,linux,bash,shell,unix,command-line,Linux,Bash,Shell,Unix,Command Line,第一次询问有关堆栈溢出的问题。我是一名学生,正在开发一个小型Linux待办事项列表应用程序,用户可以使用如下命令创建新文件: ./todo add "hello world" …结果应该是todo列表中的一个新文本文件,文本为“hello world”(或“”中的任何内容) 到目前为止,我的bash脚本add.sh创建了一个新文件,将其添加到todo列表中,并重命名该列表中的所有项目,以创建一个编号列表,如下所示: ./todo add "hello world" 1.txt 2.txt 3

第一次询问有关堆栈溢出的问题。我是一名学生,正在开发一个小型Linux待办事项列表应用程序,用户可以使用如下命令创建新文件:

./todo add "hello world"
…结果应该是todo列表中的一个新文本文件,文本为“hello world”(或“”中的任何内容)

到目前为止,我的bash脚本add.sh创建了一个新文件,将其添加到todo列表中,并重命名该列表中的所有项目,以创建一个编号列表,如下所示:

./todo add "hello world"
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参数成为新文件中的文本

现在,当我搜索新文件时,它是空白的。我想要的是新文件读为“hello world”


提前谢谢

问题出在您的
/todo
脚本中:

if [ $1 == "add" ]
then
  ./usrMenu/add/add.sh #calls add.sh
fi
您没有将参数从
/todo
传递到
add.sh
,因此
add.sh
中的
$2
将为空,并且与
todo
中的
$2
不同。要传递所有参数,可以使用
“$@”

或者,只传递您关心的参数,该参数将在脚本中变成
“$1”
(而不是“$2”):

./usrMenu/add/add.sh "$2" # Let "$2" in this script become "$1" in add.sh

/todo
add.sh
有什么关系?/todo是一个bash脚本,可以用来调用目录中的任何其他函数/脚本~/todoApp。您能发布
/todo
?听起来问题出在脚本中,而不是
add.sh
是的,谢谢!我的代码的这一部分非常基本。它只是一个if-else树,其中
if[$1==“add”]then./usrMenu/add/add.sh调用add.sh
,树的其余部分调用其他bash脚本。只需将参数传递给脚本:
/usrMenu/add/add.sh“$2”