A little guidance needed :)

Hi guys, I'm learning the ropes of BASH and am doing a few exercises to see if its sinking in but have gotten stuck on something I KNOW is looking at me right in the face but just isn't registering.

I'm creating a script that needs to get specific strings from a line. So using the "ls -l <filename>" command, i get a string with 6 fields (permissions and so forth). No i know there is a way to get an individual field from the 6 fields and store it but i just cant remember it. I believe its a pipe to the grep command but I'm unsure and for all i know there is an easier method.

Any help would be appreciated!

Code:

str = $(ls -l <filename>) #To get the info

now i just need to "split" the data - if it was perl, easy enough :P.

Edit: No AWK or SED.

This sounds like a homework question, try:

man cut

Regards

Indeed you could call it something like that.. Still if I'm not going to get anywhere sitting around and fiddling about then i may as well ask. Otherwise im not learning anything .. just wasting a bit of time.

Cheers non the less!

Code

str=$(ls -l <filename> | cut -d ' ' -f 1)

-d Delimiter (space in my case)
-f fields (1st one in my case)

Just a brief explanation if someone was stuck on a similar situation.

The default field separator is a space. -d will not break anything but it is not required.

I tried that however it did not work - brought up the entire line with out the option. Unix doesn't like me very much lol.

A final (i hope) question. If i had the work "command" and i wanted to split it into characters, how would i go about doing this ? See i need to be able to process each character individually using a loop however i am not aware of a command that allows me to do this.

Thanks again.

position=1

#put the following in a loop
#cut -c stands for column
while [ $position -ge 8 ]
do
echo `cut -c$position`
position=`expr $position + 1`
done

position=1
word="command"
#put the following in a loop
#cut -c stands for column
while [ $position -ge 8 ]
do
echo `echo $word |cut -c$position`
position=`expr $position + 1`
done

position=1
word="command"
#put the following in a loop
#cut -c stands for column
while [ $position -le 8 ]
do
echo `echo $word |cut -c$position`
position=`expr $position + 1`
done

if you are learning bash, you will go here and learn about strings in bash..
eg

# a="command"
#  echo "length of a is ${#a} "
length of a is 7

to get individual fields, i leave it to you. its all in that document.

Hey ghostdog, many thanks for that - certainly cleared up some loose ends. Yeah I'm fairly new to bash scripting however I've been forced into doing some and although the earlier sections were suprisingly easy, the later have taken a huge leap in difficulty and i find myself getting stuck quite a bit.

Has gotten me a bit worried that even though I'm not going to be utilising bash/shell scripting very often (I've been taught and learn Java & .net mainly) i have to be assessed on it.

I'm sort of working over time here to build as much of a foundation as possible but things look grim.

Thank you very much though, for the guidance - its much appreciated.