using case to do this might be a bad idea?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using case to do this might be a bad idea?
# 1  
Old 04-05-2007
using case to do this might be a bad idea?

Reading this file. I want to read all 4 fields.

If 2 is populated with a p, I want to set $TYPEP to "Printers",
if not should be empty.
If 3 is populated with an r, I want to set $TYPER to "REQ Printers"
if not should be empty.
If 4 is populated with letter o, I want to set $TYPEO to "Orders"
if not should be empty.

The problem I am having is that it looks like case only lets me fall through it once. I canot get $TYPEP $TYPER $TYPEO to reset to empty after reading each record.

Here is snippet of my file, code and output.
======
Here is snippet of my file:
=======
100:::o:
51:p:r::
52:p:r::
53:p:r::

note: my preview of snippet of the file is showing smiles. Maybe we can start a new thing, smile separated values? :cool: (Just kidding!) Not sure how to get rid of that but it is just 4 colon separated fields, 4 records
I replace colons with commas so you can read it correctly:

100,,,o,
51,p,r,,
52,p,r,,
53,p,r,,
=======
code
=======
############
doIt ()
{
echo "Starting Time:" $RTIME
echo "Cycling Servers In Domain: " ${DOMAIN}
echo ${CYCLER}
echo "Reading server List .. for domain " ${DOMAIN}
echo " Reason"
echo " P=Printers"
echo " R=Requisition Printers"
echo "Server O=Orders"
echo "ID Description P R O Node Domain"

typeset -L36 NM_SERVER
typeset -L9 TYPEP
typeset -L16 TYPER
typeset -L10 TYPEO

while read no pflag rflag oflag
do

NM_SERVER=`$GET_NM -getp \\\node\\\\$NODE_NAME\\\domain\\\\$DOMAIN\\\servers\\\\$no\\\ ServerName`

case ${pflag} in p)
TYPEP="Printers"
;;
esac
case ${rflag} in r)
TYPER="REQ Printers"
;;
esac
case ${oflag} in o)
TYPEO="Orders"
;;
esac

typeset -L4 no
typeset -L10 NODE_NAME
typeset -L6 DOMAIN
typeset -L2 pflag
typeset -L2 rflag
typeset -L2 oflag
printf ${no}${NM_SERVER}${TYPEP}${TYPER}${TYPEO}${pflag}${rflag}${oflag}${NODE_NAME}$DOMAIN
#${CYCLER} ${no} ${NODE_NAME} "-l" ${UN} ${DOMAIN} ${PW}
printf "\n"
unset no
typeset -L2 oflag
typeset -L8 NODE_NAME
typeset -L5 DOMAIN

done< ${FILE_PATH}/.${NODE_NAME}.${DOMAIN}.server.lst | tee -a $LOG
}
########
#MAIN
#######
doIt

======
Output
======
ID Description P R O Node Domain
100 ORM Format Server Orders o cerntst1 build
51 CPM Script Orders p r cerntst1 build
52 CPM Code Cache Manager Orders p r cerntst1 build
53 CPM Script Private Orders p r cerntst1 build
====
My Comments
=====
using ksh on AIX 5.3 ML04
You see thar "Orders" prints for each of the 4 lines. It should have only printed for the first. The other 3 should have printed both "Printers" and REQ Printers"
Any suggestion is appreciated. I think I am using the wrong tool. If you can point me to a more suitable function or command or tool, I appreciate it.
SkyyBugg
=====

Last edited by reborg; 04-05-2007 at 05:29 PM..
# 2  
Old 04-05-2007
Code:
case ${pflag} in 
p)
       TYPEP="Printers"
       ;;
*)
       TYPEP=""
       ;;
esac
case ${rflag} in 
r)
       TYPER="REQ Printers"
       ;;
*)
       TYPER=""
       ;;
esac
case ${oflag} in 
o)
       TYPEO="Orders"
       ;;
*)
       TYPEO=""
       ;;
esac

# 3  
Old 04-05-2007
Quote:
======
Here is snippet of my file:
=======
100:::O:
51:P:r::
52:P:r::
53:P:r::

If 2 is populated with a p, I want to set $TYPEP to "Printers",
if not should be empty.
If 3 is populated with an r, I want to set $TYPER to "REQ Printers"
if not should be empty.
If 4 is populated with letter o, I want to set $TYPEO to "Orders"
if not should be empty.
See your file is and logic is clear, but what output you want from this is not clear, can you write down you output only..
# 4  
Old 04-05-2007
Quote:
Originally Posted by Skyybugg
Reading this file. I want to read all 4 fields.

If 2 is populated with a p, I want to set $TYPEP to "Printers",
if not should be empty.
If 3 is populated with an r, I want to set $TYPER to "REQ Printers"
if not should be empty.
If 4 is populated with letter o, I want to set $TYPEO to "Orders"
if not should be empty.

The problem I am having is that it looks like case only lets me fall through it once. I canot get $TYPEP $TYPER $TYPEO to reset to empty after reading each record.

Here is snippet of my file, code and output.
======
Here is snippet of my file:
=======
100:::o:
51:p:r::
52:p:r::
53:p:r::

note: my preview of snippet of the file is showing smiles. Maybe we can start a new thing, smile separated values? :cool: (Just kidding!)

Check the box below the text area that says "Disable smilies in text".
Quote:
Not sure how to get rid of that but it is just 4 colon separated fields, 4 records
I replace colons with commas so you can read it correctly:

100,,,o,
51,p,r,,
52,p,r,,
53,p,r,,
=======
code
=======
############
doIt ()
{
echo "Starting Time:" $RTIME
echo "Cycling Servers In Domain: " ${DOMAIN}
echo ${CYCLER}
echo "Reading server List .. for domain " ${DOMAIN}
echo " Reason"
echo " P=Printers"
echo " R=Requisition Printers"
echo "Server O=Orders"
echo "ID Description P R O Node Domain"

typeset -L36 NM_SERVER
typeset -L9 TYPEP
typeset -L16 TYPER
typeset -L10 TYPEO

The typeset command is not standard. It is better to set the printing widths in your printf format string.
Quote:
while read no pflag rflag oflag

Shouldn't you set IFS to your delimiter?

Code:
while IFS=: read -r no pflag rflag oflag

Quote:
do
...

done< ${FILE_PATH}/.${NODE_NAME}.${DOMAIN}.server.lst | tee -a $LOG
}
########
#MAIN
#######
doIt

======
Output
======
ID Description P R O Node Domain
100 ORM Format Server Orders o cerntst1 build
51 CPM Script Orders p r cerntst1 build
52 CPM Code Cache Manager Orders p r cerntst1 build
53 CPM Script Private Orders p r cerntst1 build
====
My Comments
=====
using ksh on AIX 5.3 ML04
You see thar "Orders" prints for each of the 4 lines. It should have only printed for the first. The other 3 should have printed both "Printers" and REQ Printers"
Any suggestion is appreciated. I think I am using the wrong tool. If you can point me to a more suitable function or command or tool, I appreciate it.
SkyyBugg
=====

You should reset your flags before processing each line.

Code:
while ..... read ....
do
  unset pflag rflag oflag
  ...

Or:

Code:
while ..... read ....
do
   pflag= rflag= oflag=
   ...

# 5  
Old 04-06-2007
Quote:
Originally Posted by Perderabo
Code:
case ${pflag} in 
p)
       TYPEP="Printers"
       ;;
*)
       TYPEP=""
       ;;
esac
case ${rflag} in 
r)
       TYPER="REQ Printers"
       ;;
*)
       TYPER=""
       ;;
esac
case ${oflag} in 
o)
       TYPEO="Orders"
       ;;
*)
       TYPEO=""
       ;;
esac

I replaced my case section with this one and now TYPEO is populated on first row but not the rest. TYPEP and TYPER showing empty on all rows!
Not sure why. Maybe I should post entire script, but I put *alot* of documentation in there. I have high hopes for the script and doc and script are not complete and out of synch! Sometimes (alot of times!)results are what you least expect!
# 6  
Old 04-06-2007
To debug it try:
Code:
echo oflag = $oflag
case ${oflag} in 
o)
       TYPEO="Orders"
       ;;
*)
       TYPEO=""
       ;;
esac
echo TYPEO = $TYPEO

You will find that oflag was not set to "o" if TYPEO is getting set to "".
# 7  
Old 04-06-2007
That is the kind of behaviour of case is is perplexing me. I don't want to be assuming but I am attaching input, output and the entrire script. You will see in the input file towards the middle and end, oflag is set to o and the TYPO is still not populating. I wonder if TYPO isn't a reserved variable?
SkyyBugg
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why I get bad bad substitution when using eval?

Why I get bad replace when using eval? $ map0=( "0" "0000" "0") $ i=0 $ eval echo \${map$i} 0000 $ a=`eval echo \${map$i}` !!!error happens!!! bash: ${map$i}: bad substitution How to resolve it ? Thanks! (5 Replies)
Discussion started by: 915086731
5 Replies

2. Red Hat

Is overlapping two RAID5 arrays on same drives a bad idea ??

is placing two RAID5 arrays on disk as shown below Is advisable? Will this create performance problems? sda-(500GB) sdb-(1TB) sdc-(1TB) sdd-(1TB) (250MB)----------(250MB) ---------unused------------unused------->(/dev/md0) RAID1 ... (6 Replies)
Discussion started by: Saed
6 Replies

3. OS X (Apple)

Deleting a recursive symbolic link was a very bad idea

Well i was tidying up some files in a very important directory on our development server and somehow some plank had put a recursive sybmolic link in it. Which I the even bigger plank tried to delete from my FTP client. My FTP client then thought it would be OK to delete not only the sybmolic link... (0 Replies)
Discussion started by: timgolding
0 Replies

4. Shell Programming and Scripting

case loop... repeat on bad input?

I'm trying to get a case statement to start over if an undefined option is selected... But I am ata loss on how to actually do it. Here is a quick example of what I have. Echo "1) do this/n 2) Do that/n 3) Quit/n Make a selection/n" Read answer Case answer in 1) Dothid;; 2) Dothat;;... (3 Replies)
Discussion started by: trey85stang
3 Replies

5. 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

6. UNIX for Dummies Questions & Answers

Why is it Bad Idea to insert "." (Dot) to PATH ?

I was told that it's a Bad Idea (especially for root ) to Add To the Variable $PATH in unix the ":." (dot), In order to execute programs in my current directory without typing ./program For example: PATH=$PATH:$HOME/bin:. Does someone know why is it a Bad Idea? (2 Replies)
Discussion started by: amitbern
2 Replies

7. Shell Programming and Scripting

Limitations of awk? Good idea? Bad idea?

Keeping in mind that I'm relatively comfortable with programming in general but very new to unix and korn/bourne shell scripts.. I'm using awk on a CSV file, and then performing calculations and operations on specific fields within specific records. The CSV file I'm working with has about 600... (2 Replies)
Discussion started by: yongho
2 Replies

8. 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