Help writing bash script

Solved/Closed
bashbabe Posts 3 Registration date Tuesday September 22, 2009 Status Member Last seen October 25, 2009 - Sep 22, 2009 at 08:00 PM
 Stampy - Oct 5, 2009 at 08:43 AM
Hello,
I need to write a bash script that will perform the following tasks:
Create a file test.txt in the lab1 directory
If file test.txt already exists, script should inform user and create the file after deleting existing file
Change permissions of file test.txt to 755
Display exit status of the permission change command
Display contents of the lab1 directory

I am new to scripting and am practicing using the example. Any help/suggestions will be appreciated.

1 response

HI there,

i am also new to bash scripting but i think it should look something like this

--------------------------------------------------------------------------------------
#!/bin/bash

#Create a file test.txt in lab1 directory
cd /??/<path to lab1>
touch text.txt

#if file exists, Inform user, Delete existing file, Create new file
if [ -f text.txt ]
then echo "File Exists"
rm text.txt
touch text.txt
fi

#change permissions of text.txt, Display exit status
chmod 755 text.txt
echo $?

#Display contents of lab1
ls

exit 0
--------------------------------------------------------------------------------------

Hope it helps/works let me know :)

Glenn
2
bashbabe Posts 3 Registration date Tuesday September 22, 2009 Status Member Last seen October 25, 2009
Oct 4, 2009 at 09:21 PM
Stampy,

I meant to write earlier and tell you how much I appreciated your help. It worked!

Thanks.

Bashbabe
0
Stampy > bashbabe Posts 3 Registration date Tuesday September 22, 2009 Status Member Last seen October 25, 2009
Oct 5, 2009 at 08:43 AM
Not a problem bashbabe . . . if there is any other scripts you need a hand with just give us a yell ill be glad to help :)

Stampy
0