I am new to shell scripting in Linux and I am trying to take data from the keyboard and then append the data passed in to a file. Pretty straight forward but I am getting an error when I try to create the file. The error says "you do not have permission to create this file".
I first do a check to make sure the file exists. If it exists, append to the end of the file. If not, create the file. What am I doing wrong?
Thank you!
P.S. In this case, I do not have the file created yet
#!/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
you do not have permission to create this file. Linux file systems do not let arbitrary users read or write to arbitrary locations. Try changingfile="/testFile.dat"tofile="~/testFile.dat", which will put the file in your home dir.