LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-23-2010, 05:43 PM   #1
indur
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Rep: Reputation: 0
bash scripting for dummy's :)


guess what i'm new to scripting

I have this assignment for shool and i'm stuck for hours now and clueless.
So if anyone could help me out, put me on the right track.

What i want to do is make a list for the current directory, then checking if it already exists if so ask the user if he wan'ts to overwrite, if no go back, if yes, overwrite the file.
At last if the file doesn't exist i want to create it and fill it up with the list from the directory.
This is how i failed

if [ -e dirdump.dmp ]; then
echo " The file already exists , overwrite? y/n "
read input
if [$input == "n"]; then
break
elif ["$input" = "y"]; then
ls >> dirdump.dmp
fi
else
ls >> dirdump.dmp
echo -n "U dump is aangemaakt en vind u terug in : "
pwd
fi

I get the following error: script.sh: 15 [y: not found

This is when i choose to type y.

Anyone?
 
Old 04-23-2010, 06:00 PM   #2
smbell100
Member
 
Registered: Sep 2007
Location: Shetland, UK
Distribution: Slackware, Mandrake, LFS
Posts: 59

Rep: Reputation: 16
You need spaces in the brackets, so

[$input == "n"] needs to be [ $input == "n" ].

You would probably be better off with a case or select statement.
 
Old 04-23-2010, 07:15 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
See the "LinuxCommand" tutorial in my signature. It has a great section on BASH scripting.
 
Old 04-24-2010, 02:46 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I believe the previous posts will help solve your problem, but just to be a little picky,
when scripting, try and be consistent as it helps the next person reading your script to follow what is going on:
Code:
if [$input == "n"]; then #use of double equals (=) sign

elif ["$input" = "y"]; then #use of inverted commas (") around $input and single equals (=) sign
 
Old 04-24-2010, 08:04 AM   #5
indur
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Original Poster
Rep: Reputation: 0
thanks for the quick response

I have solved it by using the advise that was given here.

The assigment is going well now!

Last edited by indur; 04-24-2010 at 08:05 AM.
 
Old 04-24-2010, 08:29 AM   #6
lugoteehalt
Senior Member
 
Registered: Sep 2003
Location: UK
Distribution: Debian
Posts: 1,215
Blog Entries: 2

Rep: Reputation: 49
Just a bit mystified. Doesn't ls >> dirdump *append* rather than overwrite?

Incidentally, does the script imply you haven't got the noclobber option set?
Code:
lugo@fido:~/Documents/Science$ set|grep clobber
SHELLOPTS=braceexpand:emacs:hashall:histexpand:interactive-comments:monitor:noclobber
 
Old 04-24-2010, 08:33 AM   #7
cola
Senior Member
 
Registered: Sep 2007
Posts: 1,045

Rep: Reputation: 65
Quote:
Originally Posted by indur View Post
thanks for the quick response

I have solved it by using the advise that was given here.

The assigment is going well now!
You can mark the thread as "[SOLVED]".
 
0 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting ZAMO Linux - General 1 01-19-2009 02:25 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
Bash scripting vinoth.ilango Solaris / OpenSolaris 6 10-29-2004 04:41 AM
BASH If-then-else Scripting Help xianzai Programming 4 10-29-2004 04:09 AM
BASH scripting help Chucklez Linux - Newbie 4 12-12-2002 12:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration