|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help converting Bash to Korn
ok for the past while i have been developing a script using Cygwin which uses the Bash(bourne again shell).. i thought all was going well until i then uploaded it to the unix OS i have to use in work, which is the korn shell. now it's throwing up all sorts of errors
time is pressing and iv had many trial and error sessions.. changing brackets etcmy code is this: Code:
#!/bin/bash
while [ 1 ]
do
printf "\n begin \n\n"
dir=*
for i in $dir
do
if [ -f "$i" ]
then
arr=( "${arr[@]}" "$i" )
fi
done
test=""
file=""
i=0
while [ $i -lt "${#arr[@]}" ];
do
file=${arr[$i]}
test=`expr match "$file" '\(^bdy\)'`
if [ -n "$test" ];
then
printf "$file \t $test \t found match \n"
elif [ -z "$test" ];
then
printf "$file \t $test \n"
fi
test=""
let i=i+1
done
printf "\n finished \n"
sleep 3
arr=()
clear
done
basically, it runs in an infinite loop, scanning a particular directory, prints them out and anytime a particular file is found which begins with the letters 'bdy' it will also print 'found match'. it works perfectly in cygwin with the bash shell, now i need the odd syntax change so that the korn shell can deal with it.. can anyone please help or advise me ? thanks in advance folks, ciaran |
|
#2
|
||||
|
||||
|
Arrays are accessed differently in ksh than in bash. i.e.
Code:
To display all values:
echo ${arr[*]}
To display number of values:
echo ${#arr[*]}
To add to an array:
set -A arr VALUE1 VALUE2 VALUE3 ...
The arrays are probably whats causing you the most trouble when you move this script to ksh. |
|
#3
|
|||
|
|||
|
Quote:
cheers stanley thanks for that. 2 more things i tried for i in * do set -A arr $i done but this didn't add the files in the dir to the array and secondly i know about regegular expressions, having did them before in java and perl. the question is to do with grouping. /.*(bdy)/ with perl and java $1 would be equal to bdy but it's not working with shell scripting. was just wondering if it was possible and whats the syntax like. cheers again dude peace out ciaran |
|
#4
|
||||
|
||||
|
You're overwriting your array with a single value during each iteration. Try setting the array like so:
Code:
j=0 for i in * do arr[$j]=$i j=`expr $j + 1` done As for recalling a matched regex, I'm not sure it's possible with expr. I'm not quite sure I understand why you need to do this. You know the matched string will always be "bdy" so you can just manually insert "bdy" where it needs to go, correct? FYI: ksh has rudementary regex matching built in for file names. Your "test=blah blah" routine could be shortened by using the following if statement: Code:
if [ $file = ?(bdy*) ] then # matched code goes here fi |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Help converting Bash to Korn |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|