Visit Our UNIX and Linux User Community


If/then problems


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If/then problems
# 1  
Old 01-12-2012
If/then problems

Code:
#! /bin/bash

# ask what the user would like to do

CMD=$CMD
MBA=$MB
RS=$RS
CT=$CT

echo 

echo -n "What would you like to do??
    
REMEMBER WHEN PROGRAMMING ICP's TO SELECT CORRECT COMMAND ACCORDING TO 
NECCESSARY TYPE CODE!

    Please enter a command
    ct = program ctek
    ws = weather station
    cc = config combiner
    cr = config rec101
    cm = ctek minicom
    mr = metered unit
    sc = Sub-Combiner

"
read -e CMD
if [ "$CMD" = "" ]; then
    echo "ERROR: invalid command. Please try again."
    ./ask1.sh
fi
if [ "$CMD" = "ct" ]; then
    ./ctek_install_script
fi
if [ "$CMD" = "ws" ]; then
    echo -n "What should the MBA be? "
    read -e $MB
    ./config_weather echo $MB
fi
if [ $CMD = "cc" ]; then
    echo -n "What would you like the MBA of this combiner to be? "
    read -e $MB
    ./config_combiner echo $MB
fi
if [ $CMD = "cr" ]; then
    echo -n "What would you like the MBA of this Sub-Combiner to be? "
    read -e 
    ./config_rec101 echo $MB
fi
if [ $CMD = "cm" ]; then
    echo -n "Please press enter to start the minicom "
    read -p "Press [Enter] to start the mincom"
    ./ctek_minicom
fi
if [ $CMD = "mr" ]; then
    echo -n "What would you like the MBA of this metered unit to be? "
    read -e $MB
    echo -n "What is the ct size for this unit?  "
    read -e $CT
    echo -n "Is this unit set to load? Please press Y or N. "
    read -e $RS
    if [ "$RS" = "N" ]; then
    ./sas echo $MB $CT
    fi
    if [ "$RS" = "Y" ]; then
    ./sas echo $MB $CT
    else
    ./set_load echo $MB $CT
    fi
fi
if [ $CMD = "sc" ]; then
    echo -n "What would you like the MBA of this Sub-Combiner to be? "
    read -e 
    ./config_rec101 echo $MB
fi
if [ $CMD = "sas" ]; then
    echo -n "What would you like the MBA to be? "
    read -e $MB
    ./sas echo $MB
fi

SO, here is my situation. I have this script, and it is all just basic if/then stuff.
on lines 32-34, the if the if/then works like a charm and does exactly what I want. However on lines 55-69 when the script is run after the 3rd echo then read-e $RS, the script stops automatically, when there is more code to be run. I cannot figure it out. I also re-did the code later in the script to be a skeletal version with no echoes on lines 75-79, and it works there but not earlier in the script. I believe it is something to do with my echo/reads and somewhere along the line the info got jumbled but I cannot seem to locate the problem. any help would be great.

Thanks

UPDATE: all exec command were removed.

Last edited by tdalyman; 01-12-2012 at 02:34 PM.. Reason: please use code tags
# 2  
Old 01-12-2012
Read takes a variable name, not a variable. So this:

Code:
VAR="ASDF"
read $VAR

would set the ASDF variable, not the VAR one.

Code:
read VAR

would actually set VAR.
# 3  
Old 01-12-2012
For this particular script I do not believe i need variables because the names I defined at the beginning of the script are the only possible commands to be entered. So each if/then will go one way or another until it finds the entered input if that makes any sense. Also, is it possible to run another script like I have with two things next to it, i.e. $MB $CT? If I ran that command by itself it must be entered ./sas (MBA) (CT) where MBA=$MB and CT=$CT. It will not run correctly/at all if I use this "ask" script that i have posted.
# 4  
Old 01-12-2012
Line 63 is an "exec". That starts a new Shell.
Nothing more will happen in the current shell whatsover after an "exec".

Are you sure that you want to use "exec" at all in this script? It is not the correct way to run another script unless you want to pass total control to that script - like when you are writing some sort of locked-in menu.
# 5  
Old 01-12-2012
No I was not sure, then I found something while scouring the net to find something to help me, and it said to not use exec so i took them all out.

---------- Post updated at 03:36 PM ---------- Previous update was at 01:33 PM ----------

Thanks for all the help guys, but I figured it out. On all the lines with read -e I took out the $ before all the MB, CT, RS, etc. Works great now! Again, thanks/
# 6  
Old 01-12-2012
The very first answer you got was me explaining to you about that.

Previous Thread | Next Thread
Test Your Knowledge in Computers #174
Difficulty: Medium
The Apache 2 'Fancy indexing' directive shows the files plus the date the file was last modified, the size and the user who originally created the files.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

About ps -ef and if-else problems

I'm new in Shell Programming and Scripting, I would like to ask some questions. ps -ef | grep $appNAme | grep -v grep <-- what will it return when it find a process is running? return 1 or 0 if then exit 1 <--- if = 0 , run this ? else continue <--- if = 1 , run this ?... (5 Replies)
Discussion started by: LoAlex
5 Replies

2. UNIX for Dummies Questions & Answers

Problems with IF Else ??!!

I have written the following code in cygwin to create directories in windows based on the parameter passed. echo $1 > fullpath path1=`awk -F / '{print $1}' $fullpath path2=`awk -F / '{print $2}' $fullpath if then if then if then continue ... (4 Replies)
Discussion started by: janardhanamk
4 Replies

3. SuSE

Problems!!

good friends I am new to linux and I have the following TELNET service problem entering data devo telnet to a Windows server 2003 server and passes will not let me since the move to this server by telnet tells me the characters and thus invalidates me income, I have a service application... (1 Reply)
Discussion started by: pepetico
1 Replies

4. Shell Programming and Scripting

Problems with $?

Hello, I have the following piece of code that tries to retrieve the result of a .sh: . $HOME/prueba/Scripts/Recogida/recogida_sedra.sh resultado=$? echo "el resultado es : $resultado" if ; then echo "Se va a preprocesar los archivos" In the code of recogida.sh I have the... (8 Replies)
Discussion started by: danietepa
8 Replies

5. UNIX for Dummies Questions & Answers

Problems with using less

Hello, I am having problems with using less on Linux version 2.6.18-92.1.17.el5 (brewbuilder@hs20-bc1-7.build.redhat.com) (gcc version 4.1.2 20071124 (Red Hat 4.1.2-42)). I am using csh but have the same problems on bash. If I pipe something to less it works perfectly i.e. cat file | less... (9 Replies)
Discussion started by: z1dane
9 Replies

6. UNIX for Dummies Questions & Answers

problems with If

I'm having problems uses "if" it works fine when i do this. #!/bin/sh a= 10 qw= 2 w= 20 { if && ;then echo 3 fi } However if i try to do #!/bin/sh a= 10 (5 Replies)
Discussion started by: THM
5 Replies

7. UNIX for Advanced & Expert Users

Problems with Last

Hi, I,ve a Unixware 7.1.3 working correctly for two years ago, since a several weeks I've a problem with the command last . The information that this command return is : For example : 1.- The user root , time login : 12:15 h, time logoff 12:15 h (the real time is 14:00). Connected time is... (3 Replies)
Discussion started by: By_Jam
3 Replies

8. UNIX for Dummies Questions & Answers

Few problems

Hi how can i do this? 1) shell script which writes data and time on to a file if filesystem exceeds 70% of space. 2) make entry to cron table to run a script every 15 mins. and can anyone expplain or demonstrate the difference between variables used in inside a function and outside a... (3 Replies)
Discussion started by: vivekshankar
3 Replies

9. UNIX for Advanced & Expert Users

'make' problems (compliation problems?)

I'm trying to compile and install both most recent version of 'make' and the most recent version of 'openssh' on my Sparc20. I've run into the following problems... and I don't know what they mean. Can someone please help me resolve these issues? I'm using the 'make' version that was... (5 Replies)
Discussion started by: xyyz
5 Replies

Featured Tech Videos