Bash Script to Ash (busybox) - Beginner | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Bash Script to Ash (busybox) - Beginner

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-03-2013
sgtbobie sgtbobie is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 4 February 2013, 12:18 PM EST
Posts: 6
Thanks: 5
Thanked 0 Times in 0 Posts
Bash Script to Ash (busybox) - Beginner

Hi All,

I have a script that I wrote on a bash shell, I use it to sort files from a directory into various other directories. I have an variable set, which is an array of strings, I then check each file against the array and if it is in there the script sorts it into the correct folder.

But now I need to move this script to ash, the only part of the script that is giving my bother is the array part, as the other commands are fairly basic commands, if, mv, etc.

I've been searching around but haven't found what I am looking for. Would anyone have any Ideas on how to replace the array in my bash script.

I have an example of what I am doing, obviously in the full script the array is much longer, and there is no point in putting in what I am doing with the files as thats all good.

Essentially, I have the array set, I read the list of files then and using a while loop, I call the sortmove function with then searches the array.


Code:
DESTDIR="/destination/"
SORTDIR="/source/"
STRINGS=(
		"string1"
		"string2"
		"string3"
		)



sortmove ()
{
	found=0
	for (( i=0; i<${#STRINGS[@]}; i++ ))
	do
	if [ "${STRINGS[$i]}" == "$3" ];
	then
		do something
	fi
	done
}

ls "${SORTDIR%%/}"/*.mp4 | while read filename
	do
		some processing
		
		sortmove var1 var2 var3
	
	done

Sponsored Links
    #2  
Old 02-03-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 August 2014, 8:11 AM EDT
Location: Amsterdam
Posts: 9,313
Thanks: 268
Thanked 2,317 Times in 2,078 Posts
Try something like this (not tested):


Code:
DESTDIR="/destination/"
SORTDIR="/source/"
STRINGS="string1
string2
string 3"

sortmove ()
{
  found=0
  oldIFS=$IFS
  IFS="
"
  for str in $STRINGS
  do
    if [ "$str" = "$3" ];
    then
      do something
    fi
  done
  IFS=$oldIFS
}

for filename in "${SORTDIR%/}"/*.mp4
do
  some processing

  sortmove var1 var2 var3

done

Since you are not using the array index, I used a simple list of strings with a linefeed as the field separator..
The Following User Says Thank You to Scrutinizer For This Useful Post:
sgtbobie (02-03-2013)
Sponsored Links
    #3  
Old 02-03-2013
sgtbobie sgtbobie is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 4 February 2013, 12:18 PM EST
Posts: 6
Thanks: 5
Thanked 0 Times in 0 Posts
Thanks, for the suggestion. I'll give it a shot and will report back on it.
    #4  
Old 02-03-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 22 August 2014, 11:14 AM EDT
Location: Aachen, Germany
Posts: 4,091
Thanks: 67
Thanked 970 Times in 920 Posts
For my enlightenment - you use the linefeed just in case a string should contain a space. If that could be ruled out, a simple space as separator would do as well, wouldn't it?
Sponsored Links
    #5  
Old 02-03-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 August 2014, 8:11 AM EDT
Location: Amsterdam
Posts: 9,313
Thanks: 268
Thanked 2,317 Times in 2,078 Posts
In that case the regular IFS could be used, i.e. any combination whitespace that consists of spaces, tabs or linefeeds.. So the use of spaces or tabs as field separator would not need to be excluded...

Last edited by Scrutinizer; 02-03-2013 at 02:52 PM..
The Following User Says Thank You to Scrutinizer For This Useful Post:
sgtbobie (02-04-2013)
Sponsored Links
    #6  
Old 02-04-2013
sgtbobie sgtbobie is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 4 February 2013, 12:18 PM EST
Posts: 6
Thanks: 5
Thanked 0 Times in 0 Posts
So looks like your suggestion was spot on the mark for me. Thanks for that!!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ash busybox read command not working inside function.... tesser Shell Programming and Scripting 8 08-23-2012 12:12 PM
[Bash] Beginner at scripting Utherr Shell Programming and Scripting 1 12-14-2010 06:58 PM
Differences in BASH and ASH shells regarding if command? Bashingaway UNIX for Dummies Questions & Answers 6 09-13-2010 05:33 PM
Script which capture screen on BusyBox brain!ac Shell Programming and Scripting 7 05-11-2010 08:00 AM
Why generate "ash and bash" different output for same bash script? s. murat Shell Programming and Scripting 0 05-26-2008 07:19 AM



All times are GMT -4. The time now is 11:42 AM.