Search : in
By :

Help writing bash script

Last answer on Oct 5, 2009 1:43:19 pm BST bashbabe, on Sep 23, 2009 1:00:12 am BST 
 Report this message to moderators

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.

Best answers for « Help writing bash script » in :
[Bash]Parameters Show[Bash]Parameters Intro Example 1 Example 2 Initialize parameters Examples Example 3 Intro You can provide a script on the command line and arguments also known as parameters for its implementation. There are two categories of...
Execute a script a startup and shutdown ShowExecute a script a startup and shutdown The trick works Windows XP, Vista and 7, but the method varies depending on your edition of Windows. For professional or full editions Start the policy editor of the local group: Start Menu> Run> Type...
Create your own command on linux ShowCreate your own command on linux Creating a Bash script Turning the command active (Executable) Creating a Bash script It’s important to create a Bash script which will run your command. Let’s see: #!/bin/bash #on displays the...
Linux - The shell ShowIntroduction to the shell The command interpreter is the interface between the user and the operating system, hence the name "shell". The shell therefore acts as an intermediary between the operating system and the user thanks to command lines...
UNIX system - The shell ShowIntroduction to the shell The command interpreter is the interface between the user and the operating system, hence its name "shell". The shell therefore acts as an intermediary between the operating system and the user using command lines...

1

Stampy, on Sep 23, 2009 6:52:13 pm BST
  • +1

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

Reply to Stampy

2

bashbabe, on Oct 5, 2009 2:21:38 am BST

Stampy,

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

Thanks.

Bashbabe

Reply to bashbabe

3

 Stampy, on Oct 5, 2009 1:43:19 pm BST

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

Reply to Stampy