dead simple bash script question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting dead simple bash script question
# 8  
Old 11-11-2008
Code:
#!/bin/sh

MACNUMBER1="00-0e-12-34-ab-c1"
MACNUMBER2="00-0e-12-34-ab-c2"
MACNUMBER3="00-0e-12-34-ab-c3"

##### BEGIN SCRIPT

echo -n "
Which PC to wake?
1) PC1
2) PC2
3) PC3
4) Wake PC2, wait 40 sec, then wake PC3
Q) Quit and take no action 
-> "
read answer

until [ "$answer" = "1" ] || [ "$answer" = "2" ] || [ "$answer" = "3" ] || [ "$answer" = "4" ] || [ "$answer" = "q" ] || [ "$answer" = "Q" ] ; do
echo -n "
Which PC to wake?
1) PC1
2) PC2
3) PC3
4) Wake PC2, wait 40 sec, then wake PC3
Q) Quit and take no action
-> "
read answer
done

case $answer in

1)
echo "Waking up $MACNUMBER1 ..."
sudo /usr/sbin/etherwake -i eth1 -b $MACNUMBER1
;;
2)
echo "Waking up $MACNUMBER2 ..."
sudo /usr/sbin/etherwake -i eth1 -b $MACNUMBER2
;;
3)
echo "Waking up $MACNUMBER3 ..."
sudo /usr/sbin/etherwake -i eth1 -b $MACNUMBER3
;;
4)
echo "Waking up $MACNUMBER2 ..."
sudo /usr/sbin/etherwake -i eth1 -b $MACNUMBER2
echo "Sleeping for 40 seconds...Please wait!..."
sleep 40
echo "Waking up $MACNUMBER3 ..."
sudo /usr/sbin/etherwake -i eth1 -b $MACNUMBER3
;;
Q|q)
echo "Exiting..."
exit 0
;;

esac

# 9  
Old 11-11-2008
thanks chat - I'll give it a try!
# 10  
Old 11-12-2008
while exit

graysky, I'll post a revised version that will do the menu if you type other characters, and if you type the word "quit" it will quit...that way if the user types in somethin invalid it will just redisplay the menu again...I know this way is not very elegant but it works eh?

Code:
#!/usr/bin/bash
MAC1=00-0e-12-34-ab-c1
MAC2=00-0e-12-34-ab-c2
MAC3=00-0e-12-34-ab-c3
while [ $input1 != quit ]; do
echo "Which PC to wake??
1) PC1
2) PC2
3) PC3
4) Wake PC2, wait 40sec, then wake PC3
Q) Quit and take no action"
read input1
  if [ $input1 == 1 ]; then
  sudo /usr/sbin/etherwake -i eth1 -b $MAC1
  exit 1
fi
if [ $input1 == 2 ]; then
  sudo /usr/sbin/etherwake -i eth1 -b $MAC2
  exit 1
fi
if [ $input1 == 3 ]; then
  sudo /usr/sbin/etherwake -i eth1 -b $MAC3
  exit 1
fi
if [ $input1 == 4 ]; then
  sudo /usr/sbin/etherwake -i eth1 -b $MAC2
  echo "PC2 sent, now waiting for 40sec"
  sleep 40
  sudo /usr/sbin/etherwake -i eth1 -b $MAC3
  exit 1
fi
if [ $input1 == Q ] || [ $input1 == q ]; then
echo "later!"
exit 1
fi
done
echo  "this is the (quit) end!! c-ya!"

#end of script

basically just added a while loop that says "run this menu until the quit-code is typed" the quit code can be whatever you want.
You can make it some other unlikely string instead of quit, like quitDoneeXit1 or something like that. You can even take the "exit 1" commands out of the menu options and it will continue to run....Again, not very elegant, but very straight forward.
# 11  
Old 11-12-2008
Your program is buggy you need to learn to quote around values in a test statement.

./t: line 5: [: !=: unary operator expected
this is the (quit) end!! c-ya!

Also why do you use a billion if statements why not use elif?
# 12  
Old 11-12-2008
Yeah, got that error here as well... Smilie
# 13  
Old 11-12-2008
well??

well, like I already mentioned its not the best way. just trying to help. The asker, like me, is obviously not a huge script writer or else they wouldnt be asking.

so maybe all you gotta do is change
while [ $input1 != quit ]; do
to
while [ "$input1" != quit ]; do

then it might work. I dont have way to test it, not from an internet connected computer anyway
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Simple bash script menu

Dear Sir, May I know how do I go about adding the following feature into the script below: When user enter values other than 1,2,3,4, a) Message “Wrong entry !!! Pls select 1,2,3 or 4” is displayed b) The screen is cleared again and the menu is displayed. #!/bin/bash clear var=1... (2 Replies)
Discussion started by: fusetrips
2 Replies

2. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

3. Shell Programming and Scripting

Simple bash script help

Hi to everyone here, I'm a new user and relatively-new linuxer. I'm trying to write a script that checks if every file from a directory is present in a given list and if not, delete it. should be simple. But I think I've done half the work only: this is to create the reference list: for c... (2 Replies)
Discussion started by: dentex
2 Replies

4. Shell Programming and Scripting

need a simple bash script

to gather the cpu utilization from a system in 5 minute intervals and direct output to file. I'm new at scripting and while this seems like an easy task I'm confused on where to start. thanks for any help (1 Reply)
Discussion started by: mkeyes001
1 Replies

5. Shell Programming and Scripting

Simple bash question - 2 files together

Hello Friends, I have a simple problem but I can't seem to find a solution, perhaps you could gimme a hand here.. I have to files 1.txt which contains the following 00 01 02 03 and 2.txt which contains the following: 20 21 22 23 All I need is to concatenate these 2 files in... (2 Replies)
Discussion started by: bashshadow1979
2 Replies

6. Shell Programming and Scripting

simple bash script

I am writing a shell script in bash one of the thing I want to show is size of export /home du -sk /export/home/oracle | cut -c 1-5 echo "kbytes" when I run the script kbytes shows up in the second line, How can I append kbytes on the same line, such as 61233 kbytes please guide thanks (2 Replies)
Discussion started by: Tirmazi
2 Replies

7. Shell Programming and Scripting

Simple BASH script?

Hi guys, I'm new to the forum so forgive me if I'm sounding ... daft. I currently work in a Tech Support role. Every day we have to generate data by running around 10 .sh scripts. I was thinking instead of having to ./filename 10 times is it possible to right a new script that will run these for... (16 Replies)
Discussion started by: JayC89
16 Replies

8. Shell Programming and Scripting

Simple Bash Read File question

Hello all, I am trying to write a simple script that will parse through a text/properties file and check a couple of if statements. Here is what I have so far: FILENAME=$1 while read line do echo $line done < $FILENAME When I call ./simple.sh testfile.txt I recieve a file or... (7 Replies)
Discussion started by: lamagra
7 Replies

9. Shell Programming and Scripting

simple bash script to ftp?

Hi all has anyone got a code snippet of how i can ftp a file automatically by running a simple bash script. I have 4 things IP address xx.xxx.xx.xx username=satnam domain = app.sample.ftp password= satnam_password Im not sure how to pull these all together to ftp a file? any ideas? (1 Reply)
Discussion started by: satnamx
1 Replies

10. Shell Programming and Scripting

Simple Bash Script

I'm sure I'm doing something wrong but as I am new to bash shell scripting I'm not sure what: Here's the code webalizer.conf is sitting in the same directory as this file which is named webalizer.sh. Can someone tell me if I've got the syntax right -- it that's correct? I'm executing the... (3 Replies)
Discussion started by: xaphalanx
3 Replies
Login or Register to Ask a Question