Bash Script to Ash (busybox) - Beginner


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Script to Ash (busybox) - Beginner
# 1  
Old 02-03-2013
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

# 2  
Old 02-03-2013
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..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 02-03-2013
Thanks, for the suggestion. I'll give it a shot and will report back on it.
# 4  
Old 02-03-2013
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?
# 5  
Old 02-03-2013
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 03:52 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-04-2013
So looks like your suggestion was spot on the mark for me. Thanks for that!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash beginner

Hello so I've stored some csv data to be read into variables like this Name,Team,Shop,Shoe etc,etc,etc,etc Code: sep="," { while IFS=$sep read Name Team Shop Shoe do count=1 dirname=$Name while do ((count++)) dirname="${Name}$count" (4 Replies)
Discussion started by: darklord173
4 Replies

2. UNIX for Beginners Questions & Answers

Beginner bash - basic shell script 'while' help...

Hi everyone, first time visitor to these forums here. Keeping a long story short I've been attempting to learn how to code in bash. I have VERY little previous experience with coding languages besides simply copying and pasting batch scripts for Windows. So, with that in mind I've followed a... (4 Replies)
Discussion started by: Meta
4 Replies

3. Shell Programming and Scripting

Beginner here, how to call a bash-script from python properly?

Hi everyone, i have the following script.sh: foo='lsusb | grep Webcam | cut -c16-18' sudo /home/user/public/usbreset /dev/bus/usb/001/$foo when i try to call this script from python using subprocess.call("script.sh", shell=True) it seems that only 'sudo /home/user/public/usbreset' is being... (6 Replies)
Discussion started by: hilfemir
6 Replies

4. Ubuntu

Convert a bash to ash

hello everybody, i'm a beginner in ash and i want to convert this bash script to ash. this script send a xml file to a nagios server : #!/bin/bash PROGNAME=$(basename $0) RELEASE="Revision 0.3" print_release() { echo "$RELEASE" } print_usage() { echo "" echo "$PROGNAME... (6 Replies)
Discussion started by: mdijoux25
6 Replies

5. Ubuntu

Bash to ash port, character-matching problem

I'm trying to convert this working bash script into an Ash script, read -p "Username:" _username if ! ]]; then echo "Valid" else echo "INVALID" fi However, Ash does not recognize the "=~" character. How can I do this? Also, is there a good reference guide, so I... (5 Replies)
Discussion started by: fzivkovi
5 Replies

6. Ubuntu

Bash to Ash, errors and adjustments

I wrote Bash script and now I want to convert it to Ash. One headache is this function: do_adduser() { setaddprompt _arr_add=("Add manually" "Add via TXT" "return to main menu" "exit program") select add_action in "${_arr_add}" do case "$REPLY" in 1)... (7 Replies)
Discussion started by: fzivkovi
7 Replies

7. Shell Programming and Scripting

ash busybox read command not working inside function....

I have a script that has to execute a read command in a function, this is in an ash busybox. The code is... trapcatch () { echo "Ctl-c Detected, what do you want to do?" echo "Please choose the number of one of the following options" echo "1. Jump past this Set" echo "2. Exit... (8 Replies)
Discussion started by: tesser
8 Replies

8. Shell Programming and Scripting

[Bash] Beginner at scripting

Hi, I'm a beginner at shell scripting, just started scripting in bash a few days ago. I want to test if the command ls *.jpg returns exit code 2, and if yes I want to execute a new command ls *.jpeg, doing a test on it... and pretty much repeat the procedure. Is this correct? #!/bin/bash... (1 Reply)
Discussion started by: Utherr
1 Replies

9. UNIX for Dummies Questions & Answers

Differences in BASH and ASH shells regarding if command?

Guys I now have a script that's working in a BASH environment, however one line doesn't appear to be working on an embedded device that has a busybox therefore ASH shell. I've googled but there's very little I can find regarding the ASH shell. In BASH the following line works... if ] ;... (6 Replies)
Discussion started by: Bashingaway
6 Replies

10. Shell Programming and Scripting

Why generate "ash and bash" different output for same bash script?

Hi, For my bash script, terminal with bash is generate an OK output and program works right. already, terminal with ash have "line 48: syntax error: Bad substitution" output and program don't work. :confused: (0 Replies)
Discussion started by: s. murat
0 Replies
Login or Register to Ask a Question