Need help : KSH>BASH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help : KSH>BASH
# 1  
Old 10-20-2006
Need help : KSH>BASH

Hello,

I've written a script in KSH, but now it needs to run on a system without KSH, ie sh or bash etc, bash seems best bet, but the simplest of things don't seem to work..

The snippet below is the main issue, basically I'm reading from a dat file and putting fields into arrays..

Dat file example (vm.dat):

132.146.23.111:apitest1:/home/vmware/apitest1/apitest.vmx
132.146.23.112:apitest2:/home/vmware/apitest2/apitest.vmx
132.146.23.113:apitest3:/home/vmware/apitest3/apitest.vmx
132.146.23.114:apitest4:/home/vmware/apitest4/apitest.vmx

Script snippet (runable):

#!/bin/bash
DF=vm.dat
#IFS=":"
count=0
grep -vE "#|^$" ${DF} | awk -F: '{print $1}' | while read F1 F2 F3
do
Filer[$count]=$F1
echo ${Filer[count]} # Debug
Volume[count]=$F2
Config[count]=$F3
(( count +=1 ))
echo $count # Debug

done
#IFS=" "

echo "Filer count = ${Filer[@]} - Count= $count" # Debug


Output example:

132.146.23.111
1
132.146.23.112
2
132.146.23.113
3
132.146.23.114
4
Filer count = - Count= 0



As you can see, it gets read into the vars fine, but seems to forget once out the loop?

I'd expect to see Filer count = 4 - Count= 4#

I suspect I'm being a dumb £"$k somewhere, but to be honest I've never really used BASH but obv. I'm missing something!?

I took the IFS part out just in case that was causing issue , doubt it though!.

Help appreciated...
# 2  
Old 10-20-2006
I think that's not a question of bash/ksh.
That's because of the "while" and the use of pipes that way. The while is executing in a subshell and so, when it finishes, you cannot see the results.
Try this way:
Code:
#!/bin/bash

DF=vm.dat
#IFS=":"

count=0

while read LINE
do
   if [[ "${LINE/#\#*/}" != "" ]]; then
      Filer[$count]=$(echo $LINE | awk -F: '{print $1}')
      echo ${Filer[count]} # Debug
      Volume[count]=$(echo $LINE | awk -F: '{print $2}')
      Config[count]=$(echo $LINE | awk -F: '{print $3}')
      (( count +=1 ))
      echo $count # Debug
   fi
done < $DF

#IFS=" "

echo "Filer count = ${#Filer[*]} - Count= $count" # Debug

Notice the "< $DF".

Regards.
# 3  
Old 10-20-2006
Thanks for the response... Very strange way of doing things?

It looks like you mean that all the 'processing' has to be kept inside the loop? is that right? Is there not a way around this as it seems like alot of over processing? I've seen the declare options, but again didn't fit the bill..

...
# 4  
Old 10-20-2006
Quote:
Originally Posted by itsupplies
Thanks for the response... Very strange way of doing things?
Mine or yours? Smilie
I've just modified your script to make it work. This doesn't mean there are not other ways...

Quote:
It looks like you mean that all the 'processing' has to be kept inside the loop? is that right? Is there not a way around this as it seems like alot of over processing? I've seen the declare options, but again didn't fit the bill..

...
Nop. I did not mean that. It's not compulsory. As I said above, there are, for sure, other ways to do it.

What I mean is that parent shell won't see his child's variables Smilie

Regards.
# 5  
Old 10-20-2006
Sorry, I meant BASH way of things Smilie I've been using KSH for many many years and have a nice comfort zone, I didn't think BASH (bash-2.05-8.2) would be to far from it. Trouble is most of my functions/loops are base on the way it works in KSH, so to suddenly not be able to hold onto VAR defs from loop to loop means big headaches Smilie

Once you setup a VAR in KSH, its pretty much global from go, is there anyway to acheive this in BASH?

Thanks again for your time Smilie
# 6  
Old 10-20-2006
Well, that's the way bash behaves. Anyway, you can use named pipes:

Code:
#!/bin/bash
DF=vm.dat
#IFS=":"
count=0
mkfifo pipe
grep -vE "#|^$" ${DF} | awk -F: '{print $1}' > pipe & 
while read F1 F2 F3
do
   Filer[$count]=$F1 
   echo ${Filer[count]} # Debug
   Volume[count]=$F2
   Config[count]=$F3
   (( count +=1 ))
   echo $count # Debug
done < pipe
rm pipe
#IFS=" "

echo "Filer count = ${Filer[@]} - Count= $count" # Debug

Perhaps this is the easiest way to port your ksh scripts to bash. Notice that now there's no subshell... Smilie

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Changing from bash to ksh

Hello, I want to run a script written in ksh but my default shell is bash as root e.g in the script it has #!/bin/ksh i have gone into /etc/passwd to change it from :/bin/bash to /bin/ksh but still giving me an error when running scripts such as ./installer -bash: ./installer: /bin/ksh:... (3 Replies)
Discussion started by: DOkuwa
3 Replies

2. Shell Programming and Scripting

Bash to ksh problem

Hi all Below code works in bash but it is not working in ksh. enddate=`date -d "$enddate + $i day" "+%Y_%m_%d"` Please help me how it works in ksh Thanks (10 Replies)
Discussion started by: pmreddy
10 Replies

3. Shell Programming and Scripting

BASH to KSH

I have a script in KSH and now need to incorporate this into another script which is in BASH. OUr script contains code like below in good number of places. Eg: echo “A B C” | read VAR1 VAR2 VAR3 This works only in ksh and not in BASH. Please let me know 1. Which is the equivalent... (3 Replies)
Discussion started by: cvsanthosh
3 Replies

4. Shell Programming and Scripting

ksh vs bash

what is diff between KSH and Bash can you tell me some commands which run in either of two but not in both. while doing normal shell programming I am unable to find diffrence between two (2 Replies)
Discussion started by: pasricha.kunal
2 Replies

5. UNIX for Dummies Questions & Answers

bash preferred or ksh with bash features

I'm a user on a fairly locked down sys V server. By default, I have ksh set as my default shell. I added to my .profile: bash -o vi so when I login, it goes into bash so I can take advantage of tab completion and use the up key to bring up previous commands. However, whenever I want to exit, I... (2 Replies)
Discussion started by: mrwatkin
2 Replies

6. Solaris

ksh vs bash

Hi, I've a general question regarding shell. I 've seen that every where i worked in production environment people are using ksh .. but i like to use .. bash .. is there any particular reason why hardcore sysadmins use ksh ? (8 Replies)
Discussion started by: fugitive
8 Replies

7. UNIX for Dummies Questions & Answers

Specified Ksh but executing in bash

Hi, I am a new bie to unix shell programming. I have specified ksh as the first line in my script but when executed it complains. *********** Script=test******************** #!/usr/bin/ksh echo hello print -p 123 ************************************ ++++++++ Execution... (7 Replies)
Discussion started by: akhilnagpal
7 Replies

8. Shell Programming and Scripting

ksh to bash mode

Hi guys... I have a ksh shell by default, so when I login to my unix box, all my .profile statements gets executed(including the aliases). But for some reasons(may be not comfortable using ksh), I would always switch to BASH(in the same session but as a child process) but in the process I am... (16 Replies)
Discussion started by: anduzzi
16 Replies

9. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

10. Shell Programming and Scripting

Ksh vs Bash

Hi Buddy, Can any one help me to overcome from the below problem? #/usr/bin/ksh typeset -Z dd dd=1 echo $dd =========== out put for the above is 01 same script I'm migrating to bash but typeset -Z option is not found in bash, Pls get me the equivalent option in BASH Thanks in... (1 Reply)
Discussion started by: krishna
1 Replies
Login or Register to Ask a Question