0

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
1
  • 1
    Sounds like your problem is you do not have permission to create this file. Linux file systems do not let arbitrary users read or write to arbitrary locations. Try changing file="/testFile.dat" to file="~/testFile.dat", which will put the file in your home dir. Commented Mar 24, 2013 at 22:05

2 Answers 2

3

You're trying to write to the file /testFile.dat which is located in the root directory /. It is highly likely that as a regular user you would not have write permissions for creating such a file.

But what you wanted I'm guessing is to create the testfile.dat in the current directory.

Replace the following line:

file="/testFile.dat"

with:

file="./testFile.dat"
Sign up to request clarification or add additional context in comments.

1 Comment

That makes sense. What a silly mistake on my part :) Thank you! Will accept
3

You are creating the file at the root. Try file="~/testFile.dat" to create the file in your home or just file="./testFile.dat" to create it in the current directory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.