LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2010, 01:28 PM   #1
SilversleevesX
Member
 
Registered: May 2009
Posts: 181
Blog Entries: 9

Rep: Reputation: 15
Best place to learn about BASH arrays


ABS stumps me, frankly -- it seems focussed on the types of arrays that either throw errors, distract or plain old don't suit my purposes.
Which are (at the moment):
Trying to relieve a script of its burden of if/then/fi loops by setting variables to data (strings and sub-strings) read in from text files as arrays and splitting the lines with builtin commands. Everyone says (and it's been proven to me here on the LQ forums) that this is speedier, if not always as accurate, than using externals like "cut".

At last, folks, BZT wants to start from arrays and work in the opposite direction from what has become his regrettable routine. The problem is, as I noted in the first line of this post, every Web resource, how-to and advice thread in forums I've so far come across seems to have a starting point of "put your array in the script and iterate from there, Charlie Brown!" I am beginning to wonder if the "miracle" of arrays isn't just a bunch of bush-whah.

So prove me wrong.

BZT

The script as it's currently written:
Code:
#!/bin/bash
while read 'line';
do
		file=$(echo $line)
		filename=$(basename "$file")
		k=$(echo $filename | cut -d'-' -f2)
		linecheck=0
	while read 'line';
	do
		sp=$(echo $line)
		vc=$(echo $sp | cut -d":" -f1)
		cv=$(echo $sp | cut -d":" -f2)
		if grep -q $vc <<<$k; then
			matchX=$filename
    		echo -e "I match $matchX with sitecode '$vc': \033[1;36m$cv.\033[0m"
			echo -e "$file:$cv">>sitematch.txt
		fi
#		linecheck=$[linecheck+1]
#		if [ "$linecheck" -eq "$axe" ];
#		then
#			echo -e $file>>nomatches.txt
#		fi
	done<codesites.txt
done<nolabel.txt
nolabel.txt:
Code:
gae67-6896-036-593.jpg
gae68-6896-036-942.jpg
gae69-6900-080-005.jpg
gae72-7225-030-120.jpg
gae72-7225-234-136.jpg
gae73-7324-059-005.jpg
gae73-7380-278-534.jpg
gae77-7712-145-926.jpg
gae77-7712-147-498.jpg
gae77-7712-148-563.jpg
gae77-7712-150-296.jpg
gae77-7712-157-775.jpg
gae77-7712-158-781.jpg
gae77-7748-080-618.jpg
gae77-7748-272-637.jpg
Selected lines from codesites.txt:
Code:
6896:BEACH NUDITY FROM PRIVATE PROFILES
7017:ABANDONED PLACES
7057:ROCK & ROLL GIRLS
7225:MY GF IN A CAR
7320:SWINGERS EVENING PARTY
7380:BUSTY MOMMIES
7477:BEACH MERMAIDS
7479:HOT SUMMER
7712:CURVY VOLUPTUOUS WOMEN
7751:AMATEUR HOTTIES
7752:PROUD NATURAL

Last edited by SilversleevesX; 09-17-2010 at 01:41 PM.
 
Old 09-17-2010, 03:04 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Well, it is called the "Advanced" Bash Scripting guide for a reason. But it can be a good place to learn from all the same. The best study technique is simply to try out the examples yourself and see what happens. Then play around with them to learn more.

Or you could start with the Bash Guide For Beginners and work your way up.

Anyway, an array is simply a variable with multiple entries, each one designated by an index number (or string, in bash 4's new associative arrays).

Let's start with a simple example.
Code:
#Set 3 array elements directly, just like regular variables.  Array indexes start at 0.
MyArray[0]="Fry"
MyArray[1]="Bender"
MyArray[2]="Leela"

echo "${MyArray[2]} ${MyArray[1]} ${MyArray[0]}"
#output is: Leela Bender Fry


#The @ mark will give you all array elements at once.
echo "${MyArray[@]}"

#output is: Fry Bender Leela
There are a variety of ways to set an array, with the most common one being:
Code:
#Set a string to multiple array elements at once
MyArray=( This is a text string )

echo ${MyArray[0]}  #output: This

echo ${MyArray[3]}  #output: text

#Iterate through the array elements backwards
for i in 4 3 2 1 0; do 
  echo ${MyArray[$i]}
done

#output:
string
text
a
is
This
In the above setting, each word inside the parentheses becomes one array element, with word splitting done based on the IFS variable.

Hopefully that will be enough for you to understand how they work, and you can go back to ABS to learn the details.

I also recommend learning about parameter substitution. The various forms there can also be used for trimming strings down to their useful parts. For example, you can use it as a replacement for basename.
Code:
path=/home/david/documents/myfile
filename=${path##*/}
echo $filename
#result: myfile
Most of the parameter substitutions can be used with array elements too, of course.
 
Old 09-17-2010, 03:20 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
By the way, I always cringe when I see lines like this:
Code:
file=$(echo $line)
There's absolutely no need to use echo or command substitution here. Just set the darn thing directly.

So here's the first section of your script using parameter substitutions and an array (untested).
Code:
file=$line
filename=${file##*/}
IFS="-"
karr=( $filename )
k=${karr[1]}
unset IFS karr
It's a bit more typing, but it should be faster in execution.

Edit: Since there's more than one way to skin a cat, you could also simply use this instead of an array to set the k variable (assuming uniform filenames):
Code:
k=${filename#*-}
k=${k%%-*}

Last edited by David the H.; 09-17-2010 at 04:06 PM. Reason: as stated
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash and arrays disca Programming 3 07-27-2010 08:26 AM
LXer: Learn Linux, 101: Find and place system files LXer Syndicated Linux News 0 06-10-2010 10:41 AM
Bash Arrays Simon256 Programming 2 02-17-2009 01:39 PM
Awesome place to learn linux :) nirjharoberoi LQ Suggestions & Feedback 1 12-17-2007 04:25 AM
Where's the best place to learn Bash? evangelinux Programming 5 06-13-2004 11:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration