LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 08-25-2003, 11:07 PM   #1
TheRealDeal
Member
 
Registered: Jun 2003
Location: Central Coast, NSW, Australia
Distribution: Gentoo
Posts: 438

Rep: Reputation: 30
Easy bash script question




Hello,

I have made a basic script which installs a whole bunch of programs, from 3 different cds. I'm new to scripting but I have it all working fine. What I would like to add to the script is some kind of checking, which will basically check for a file to ensure the correct CD is in.

At the moment the script prompts for CD 2 for example, if you put CD 2 in, it all works fine, but if you put the wrong CD in, it still goes ahead with the script but stuffs up because none of the files are there.

I want the script to do something like look for a file on the CD's, for example CD2.txt, if that file is there, will go ahead with the script, but if it isn't, echo's "Wrong CD" or something to the screen.

Can any one please point me in the right direction?

Thanks alot.

>Craig
 
Old 08-26-2003, 12:51 AM   #2
jalal
Member
 
Registered: Aug 2003
Location: .bh
Distribution: Gentoo
Posts: 188

Rep: Reputation: 30
you can check if a file exists by using -e

e.g.

if [ -e /mnt/cdrom/CD2.txt ] then your code
 
Old 08-26-2003, 01:17 AM   #3
TheRealDeal
Member
 
Registered: Jun 2003
Location: Central Coast, NSW, Australia
Distribution: Gentoo
Posts: 438

Original Poster
Rep: Reputation: 30
Thanks jalal !!

I'll give it a go. so in theory i can do something like...

if -e /mnt/cdrom/cd2.txt
then [continue with normal script]
else echo "wrong cd inserted"
fi

i'll check out the man page anyway.

if cd2.txt didn't exist, would it prompt for to enter the cd again or would they have to restart the script again?

Sorry about all the questions.

>Craig
 
Old 08-26-2003, 02:16 AM   #4
jalal
Member
 
Registered: Aug 2003
Location: .bh
Distribution: Gentoo
Posts: 188

Rep: Reputation: 30
this is what the "your code" bit is going to decide
 
Old 08-26-2003, 02:27 AM   #5
TheRealDeal
Member
 
Registered: Jun 2003
Location: Central Coast, NSW, Australia
Distribution: Gentoo
Posts: 438

Original Poster
Rep: Reputation: 30


Thanks

This is what I've got, and it works nearly....

if [ -f /mnt/cdrom/cd2.txt ]
then
{continue's with script}
else
echo "Wrong CD inserted".
fi


This works to a point. What I would like to do is, after the 'then' make it goto a line below which continues with the script.

And if they have put the wrong cd in, I want it to echo "wrong CD, insert correct CD and press enter again"

is this possible, is there a goto type command in bash scripting? and would I need some kind of loop.

I'm lost
 
Old 08-26-2003, 05:12 AM   #6
jalal
Member
 
Registered: Aug 2003
Location: .bh
Distribution: Gentoo
Posts: 188

Rep: Reputation: 30
don't panic just yet

well, I'm no bash guru, but I tried this:

until [ -f /mnt/cdrom/cd2.txt ]; do
read -p 'please insert cd 2 and press any key to continue ... ' -n 1 </dev/tty
done


see if it works for you.
 
Old 08-26-2003, 06:57 PM   #7
TheRealDeal
Member
 
Registered: Jun 2003
Location: Central Coast, NSW, Australia
Distribution: Gentoo
Posts: 438

Original Poster
Rep: Reputation: 30
Jalal, you are a legend!!!!

Thanks SO much. It's doing exactly what I want it to do now. I owe you one.

Thanks again for all your help, you rock!!!

>Craig
 
Old 08-26-2003, 08:42 PM   #8
TheRealDeal
Member
 
Registered: Jun 2003
Location: Central Coast, NSW, Australia
Distribution: Gentoo
Posts: 438

Original Poster
Rep: Reputation: 30
Jalal,

Just 1 more question if that's ok. The script is 99% there

This is the first part of the script....

-------------------------------------------------
clear

# Prompt user for SC 3.0 CD.
eject
echo " "
echo "Please insert Server Centre 3.0 CD and press Enter to continue."
read -s

# Mount SC 3.0 CD.
mount /mnt/cdrom

# Check to ensure correct CD is inserted.
until [ -f /mnt/cdrom/cd2.txt ]; do
read -p 'Wrong CD inserted, please insert Server Centre 3.0 CD and press Enter to continue.' -n 1 </dev/tty | eject;
done

# Install modutils package.
echo " "
echo "Installing modutils package..."
rpm -ivh /mnt/cdrom/Utilities/modutils*rpm
------------------------------------------

Is there anyway to add into the "Check to ensure..." section, a way to loop the script back up to the 'mount' part just above it if the cd2.txt file isn't there?

As it stands, the script checks to make sure the right cd is in, if it is, it continues with the various rpm installs, if the file isn't there, it echo's "wrong cd..." to the screen, ejects the 'wrong cd' but then gets stuck in the 'until' process (every time Enter is pressed, it echo's "wrong cd" to the screen). Is there any way when Enter is pressed after the wrong cd has been ejected, it can jump back up to the earlier mount command? or mount the cd again and run the until section again?

Sorry if this is confusing.

You've been a great help!!!

>Craig
 
Old 08-26-2003, 10:16 PM   #9
jalal
Member
 
Registered: Aug 2003
Location: .bh
Distribution: Gentoo
Posts: 188

Rep: Reputation: 30
glad to be of service

you can just put the mount line in the until loop;

until ....
read ...
mount /mnt/cdrom
done
 
Old 08-26-2003, 10:27 PM   #10
slapNUT
Member
 
Registered: Jun 2001
Location: Recycle Bin
Distribution: Linux & Everything else on VirtualBox
Posts: 144

Rep: Reputation: 15
# Check to ensure correct CD is inserted.
until [ -f /mnt/cdrom/cd2.txt ]; do
eject
echo " "
echo "Please insert Server Centre 3.0 CD and press Enter to continue."
read
mount /mnt/cdrom
done
 
Old 08-26-2003, 10:33 PM   #11
TheRealDeal
Member
 
Registered: Jun 2003
Location: Central Coast, NSW, Australia
Distribution: Gentoo
Posts: 438

Original Poster
Rep: Reputation: 30
Thanks guys!!!

I'll give it all a go. Sorry about all the questions!!



Thanks
Craig
 
Old 08-26-2003, 10:45 PM   #12
TheRealDeal
Member
 
Registered: Jun 2003
Location: Central Coast, NSW, Australia
Distribution: Gentoo
Posts: 438

Original Poster
Rep: Reputation: 30
WORKS!!!!!

you legends!!!!

>Craig
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Easy Shell Script Question scriptn00b Linux - Newbie 8 09-04-2005 08:09 AM
easy script question citrus Linux - Newbie 7 03-23-2005 04:58 PM
Easy Bash Script Question skibud2 Linux - Newbie 1 11-17-2004 12:14 PM
Bash script!! need help!!!!!could be easy! yulitao Programming 3 03-25-2004 07:52 PM
Easy script question. TheRealDeal Linux - General 2 08-22-2003 12:51 AM


All times are GMT -5. The time now is 10:07 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration