这是我的第一个bash程序,我正在做作业。我试图在new()函数中创建一个文件,并使用第一个命令行参数中作为用户输入提供的文件名。
我尝试将第一个命令行参数存储在函数中的“文件”变量中。然后,我尝试使用$ file创建文件。
#!/bin/bash
new() {
file=$1
touch $file
if [ $# -gt 2 ]
then
echo $3 > $file
else
echo "Untitled database" > $file
fi
}
if [ $# -eq 0 ]
then
echo "Error: Must enter filename as first argument."
exit 1
fi
if [ $2 = "new" ]
then
new
fi
我希望程序创建一个具有第一个命令行参数中指定名称的文件,如果没有提供第三个参数,则文件中的第一行要么是第三个命令行参数,要么是“无标题数据库”。 / p>
但是,我当前的输出包含此错误。似乎file = $ 1并未将我的文件变量设置为任何值。我不确定自己在做什么错。
./db.sh DB new
touch: missing file operand
Try 'touch --help' for more information.
./db.sh: line 14: $file: ambiguous redirect