UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 6th, 2006, 02:42 AM
GlobeTrotter GlobeTrotter is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 70 GlobeTrotter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 12 m 53 sec
Reputation Power: 4
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 etc

my 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

Reply With Quote
  #2  
Old April 6th, 2006, 08:56 AM
stanleypane's Avatar
stanleypane stanleypane is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Baltimore, MD
Posts: 208 stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 7 h 4 m 19 sec
Reputation Power: 21
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.

Reply With Quote
  #3  
Old April 6th, 2006, 02:51 PM
GlobeTrotter GlobeTrotter is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 70 GlobeTrotter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 12 m 53 sec
Reputation Power: 4
Quote:
Originally Posted by stanleypane
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.


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

Reply With Quote
  #4  
Old April 6th, 2006, 04:30 PM
stanleypane's Avatar
stanleypane stanleypane is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Baltimore, MD
Posts: 208 stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level)stanleypane User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 7 h 4 m 19 sec
Reputation Power: 21
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Help converting Bash to Korn


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT