decision, case, it then else?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting decision, case, it then else?
# 1  
Old 01-15-2002
decision, case, it then else?

I'm writing this script in Korn Shell, on AIX. The script will print a log file, and it needs to decide what region it's in before printing.

( the output of db2 "get instance" is either
"The current database manager instance is: db2q" or
"The current database manager instance is: db2u")

I have tried this, also an if statement, but I havent' got it to work yet, any ideas?

thanks
JP

echo "enter log number"$
read slog$
$
db2 "get instance" > reg$$$
grep "u$" reg$$ > regfile$
if [ -s regfile ]; then$
qu=u$
fi$
if [ -z regfile ]; then$
qu=q$
fi$
case $qu in $
u)$
cd /dir1/db2u/apps/log; ls|grep $slog > newlog; cat newlog|xargs cat $
$
rm newlog ;;$
q)$
cd /dir1/db2q/apps/log; ls|grep $slog > snewlog; cat snewlog|xargs cat$
rm snewlog ;;$
$
*) echo "bleh!" ;;$
esac$
$
rm reg$$; rm regfile$
# 2  
Old 01-30-2002
Question if you still need...

You will need to double check the syntax, but you might try something like this....

echo "enter log number"
read slog
db2inst=$(db2 "get instance") #cmd sub in ksh
if [ $db2inst = db2u ]; then
od /dir.../log && ls|grep $slog > newlog; cat..
rm newlog
else
od /dir.../log && ls|grep $slog > ...; cat ...rm snewlog
####

Just ideas. The "&&" causes the second command to only run if the first is successfull.
# 3  
Old 01-30-2002
I'm just writing this out off the top of my head, so it may not be correct, but try something like this:
Code:
#!/usr/bin/ksh

echo "Enter the log number: \c"
read slog
if [ -z "$slog" ]; then echo "No number entered."; exit 1; fi

echo "Do you want to [V]iew, or [P]rint the log? \c"
read v_p
case $v_p in
v|V) command="more" ;;
p|P) command="lp" ;;
*) echo "Not a valid action. Exiting."; exit 1 ;;
esac

db_tmp=$(db2 "get instance")
if [ -z "$db_tmp" ]; then echo "Error getting db name."; exit 1; fi
db_name=${db_tmp##*:}

cd /dir1/${db_name}/apps/log
cat *${slog}* 2>/dev/null || echo "Files with log number $slog not found" | ${command}

I *think* this should handle a similar job, although, once again, it hasn't been tested.
You'll notice a wierd line:
db_name=${db_tmp##*:}
That should drop anything before the colon... good ol' ksh...

Hope this helps...
# 4  
Old 01-30-2002
thanks everyone, I took a piece of this and that, and ended up with a working script:


db2 "get instance" > /tmp/answer$$
grep db2q /tmp/answer$$ 1> /dev/null
if [ "$?" = 0 ]; then
cd /top/db2q/apps/log
else
cd /top/db2u/apps/log
fi

cat *$slog*
rm /tmp/answer$$


is there a more elegant way to test for a 0 value exit status?
# 5  
Old 01-31-2002
You could save yourself a few processes by simply grabbing those lines that I used:

db_name=$(db2 "get instance")
cd /top/${db_name##*:}/apps/log
cat *${slog}*

Faster, more concise, and no "if" statements, nor a disk write and a grep. If you're going to use the Korn shell, why not use some of the builtin features to make your life easier, right?
# 6  
Old 01-31-2002
yes, your code is better, but my system didn't like the substitution in the path, cd /top/${db_name##*:}/apps/log. The system is AIX, and I am forced to use the Korn shell, that's the only one that will work, I'd like to take more advantage of it's built in features.

thanks again
# 7  
Old 01-31-2002
Really? Hmm, well, perhaps you could do it a similar way:

db_tmp=`db2 "get instance"`
db_name=`echo $db_tmp | awk -F": " '{print $2}'`

Then, either do this:
cd /top/$db_name/apps/log/

or this, if the above doesn't work:

case $db_name in
db2q) cd /top/db2q/apps/log/ ;;
db2u) cd /top/db2u/apps/log/ ;;
*) echo "Eek." ;;
esac

I'm not saying that you're doing it wrong - I guess I just feel uneasy with grepping a string out of a file... I usually try to touch the disk as little as possible in a small script.

Hmm, what do you think about this:

db2 "get instance" | grep "db2q" >/dev/null 2>&1
case $? in
0) cd /top/db2q/apps/log ;;
1) cd /top/db2u/apps/log ;;
*) echo "This should never happen." ;;
esac

That way is very similar to how your are doing it now, and I find case statements a little cleaner than if statements, but that's just my opinion.

Well, I hope this is helping at least a little...

It just goes to show how many ways there are to accomplish a task in Unix!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change first letter of a word from lower case to upper case

Hi all, I am trying to find a way to change first letter in a word from lower case to upper case. It should be done for each first word in text or in paragraph, and also for each word after punctuation like . ; : ! ?I found the following command sed -i 's/\s*./\U&\E/g' $@ filenamebut... (7 Replies)
Discussion started by: georgi58
7 Replies

2. Shell Programming and Scripting

Conversion from Upper Case to Lower Case Condition based

Hello Unix Gurus : It would be really appreciative if can find a solution for this . I have records in a file . I need to Capitalize the records based on condition . For Example i tried the following Command COMMAND --> fgrep "2000YUYU" /export/home/oracle/TST/data.dat | tr '' ''... (12 Replies)
Discussion started by: tsbiju
12 Replies

3. Shell Programming and Scripting

[Solved] Change Upper case to Lower case in C shell

Is there a command that can switch a character variable from UPPER case to lower case? like foreach AC ( ABC BCD PLL QIO) set ac `COMMAND($AC)` ... end Thanks a lot! (3 Replies)
Discussion started by: rockytodd
3 Replies

4. Shell Programming and Scripting

sed ignoring case for search but respecting case for subtitute

Hi I want to make string substitution ignoring case for search but respecting case for subtitute. Ex changing all occurences of "original" in a file to "substitute": original becomes substitute Origninal becomes Substitute ORIGINAL becomes SUBSTITUTE I know this a little special but it's not... (1 Reply)
Discussion started by: kmchen
1 Replies

5. Shell Programming and Scripting

data array needs to change upper case to lower case

Hi all, i have a data array as followes. ARRAY=DFSG345GGG ARRAY=234FDFG090 ARRAY=VDFVGBGHH so on.......... i need all english letters to be change to lower case. So i am expecting to see ARRAY=dfsg345ggg ARRAY=234fdfg090 ARRAY=vdfvgbghh so on........ If i have to copy this data in... (8 Replies)
Discussion started by: usustarr
8 Replies

6. Shell Programming and Scripting

Script to Convert Upper case to Lower case

Hi All I have a script which extracts values from a Database (A persons name) and puts it into a variable in my script IE: $NAME However the Value in the DB is all in uppercase and contains the users first name and last name EG: > echo $NAME GRAHAM BOYLE > What I need is only the... (7 Replies)
Discussion started by: grahambo2005
7 Replies

7. Shell Programming and Scripting

convert upper case to lower case in ascript

I have a package to install and the installation script which does it . The files/directories names in the script are all lower case but the actual package has everything in upper case - file names, directories . I don't want to rename directories and files in the package - it has a lot of them . ... (2 Replies)
Discussion started by: vz6zz8
2 Replies

8. Shell Programming and Scripting

Script needed to select and delete lower case and mixed case records

HELLO ALL, URGENTLY NEEDED A SCRIPT TO SELECT AND DELETE LOWER AND MIXED CASE RECORDS FROM A COLUMN IN A TABLE. FOR EXAMPLE : Table name is EMPLOYEE and the column name is CITY and the CITY column records will be: Newyork washington ... (1 Reply)
Discussion started by: abhilash mn
1 Replies

9. UNIX for Dummies Questions & Answers

lower case to upper case string conversion in shell script

How can convert a Lower case variable value to an upper case in the kron shell script. (3 Replies)
Discussion started by: dchalavadi
3 Replies
Login or Register to Ask a Question