我是Linux中的shell脚本新手,我试图从键盘获取数据,然后将传入的数据附加到文件中。很简单,但我在尝试创建文件时遇到错误。错误提示“您没有权限创建此文件”。创建文件外壳脚本
我首先进行检查以确保文件存在。如果存在,则追加到文件末尾。如果没有,请创建该文件。我究竟做错了什么?
谢谢!
P.S.在这种情况下,我还没有创建文件
#!/bin/sh
echo "Please enter your first name";
read first
echo "Please enter your last name";
read last
combine=":$first $last"
file="/testFile.dat"
if [ -f "$file" ]
then
echo "$file found."
echo $combine >> $file
else
echo "$file not found. Will create the file and add entry now."
touch $file
$combine >> $file
fi
听起来像是你的问题是'你没有权限创建此file'。 Linux文件系统不允许任意用户读取或写入任意位置。尝试将'file =“/ testFile.dat”'更改为'file =“〜/ testFile.dat”',这会将该文件放入您的主目录。 – phs 2013-03-24 22:05:21