Variable with several values and spaces.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable with several values and spaces.
# 1  
Old 02-17-2011
Variable with several values and spaces.

Hello all. I am a newb obviously and a bit stumped on this, so any help gratefully accepted.

The script is extracting metadata from individual mp3 files, then (hopefully will be) sorting them into newly-created subdirectories. I have filtered out the relevant metadata and have the album names listed in a temporary file, one entry per line, like this -

Code:
"Elvis Presley"
"Elvis Presley"
"Pot Luck with Elvis"
"Kid Galahad And Girls! Girls! Girls! (Elvis Double Features) [REMST]"

So what I am trying to do from here is create a directory for each line.
So far I have this-

Code:
#make a new subdirectory for each album
for VARIA in $ALBUMNAMES ;
do
  mkdir $2/$VARIA
done

which splits everything by individual word like this -

Code:
drwxr-xr-x 2 root root 4096 Feb 17 20:49 (Elvis
drwxr-xr-x 2 root root 4096 Feb 17 20:49 And
drwxr-xr-x 2 root root 4096 Feb 17 20:49 Double
drwxr-xr-x 2 root root 4096 Feb 17 20:49 Elvis
drwxr-xr-x 2 root root 4096 Feb 17 20:49 Features)
drwxr-xr-x 2 root root 4096 Feb 17 20:49 Galahad
drwxr-xr-x 2 root root 4096 Feb 17 20:49 Girls!
drwxr-xr-x 2 root root 4096 Feb 17 20:49 Kid
drwxr-xr-x 2 root root 4096 Feb 17 20:49 Luck
drwxr-xr-x 2 root root 4096 Feb 17 20:49 Pot
drwxr-xr-x 2 root root 4096 Feb 17 20:49 Presley
drwxr-xr-x 2 root root 4096 Feb 17 20:49 [REMST]
-rw-r--r-- 1 root root  692 Feb 17 20:49 initial_output
drwxr-xr-x 2 root root 4096 Feb 17 20:49 with

..which is obviously not what I want. How can I get the for loop to run once for each line of input rather than once for each word?

Thanks in advance

spoov
# 2  
Old 02-17-2011
See if this works for you:
Code:
while read mLine
do
  echo "mLine = <"${mLine}">"
done < input_file

# 3  
Old 02-17-2011
for treats things as words:

(from ksh man page)

Code:
       for vname [ in word ... ] ;do list ;done
              Each  time  a for command is executed, vname is set to the next word taken from the in word list.  If in
              word ...  is omitted, then the for command executes the do list once for each positional parameter  that
              is  set starting from 1 (see Parameter Expansion below).  Execution ends when there are no more words in
              the list.

Use while instead.

Code:
cat file1 | while read ALBUMNAME; do
  mkdir "$ALBUMNAME"
done

Replace cat file1 here with whatever produces your list.

i.e.

Code:
something_that_produces_my_album_list | while read ALBUMNAME
...
...

If whatever produces that list has the literal quotes (") around the names, you can use eval mkdir ..., or otherwise remove them.
# 4  
Old 02-17-2011
@Shelllife - I don't know where to start with that one sorry.


@Scottn - thanks but that doesn't seem to work -

Code:
cat initial_output | grep Album | sed 's/Album: //' | sed 's/^.*/"&/' | sed 's/.*$/&"/' | while read $ALBUMNAME;
  do
   mkdir "$ALBUMNAME"
  done

gives:

Code:
kermit:~/shelltestingground # /home/spoovy/bin/unstable/mp3sort.sh /home/spoovy/testfiles/ .
mkdir: cannot create directory `': No such file or directory
mkdir: cannot create directory `': No such file or directory
mkdir: cannot create directory `': No such file or directory
mkdir: cannot create directory `': No such file or directory
kermit:~/shelltestingground # ls
initial_output
kermit:~/shelltestingground #

# 5  
Old 02-17-2011
What's with all the pipes? You want to create directories that have quotes in the names?

Code:
grep Album initial_output | while read ALBUM; do
  mkdir "${ALBUM/Album: /}"
done

This User Gave Thanks to Scott For This Post:
# 6  
Old 02-17-2011
Spoovy, you wrote:
Quote:
...have the album names listed in a temporary file, one entry per line...
Then:
Code:
while read mLine
do
  echo "mLine = <"${mLine}">"
done < The_Name_Of_Your_Temporary_File_Here

# 7  
Old 02-18-2011
scottn - the filtering was in order to get 'clean' album names from messy metadata. By adding quotes I thought it might make my loop process recognise one record per line rather than one per word; but it obviously did't work.

Your suggestion worked though, thanks. I didn't know exactly how/why yet, but i'll look into it!

Thanks!



Shell_life - thanks for your suggestion. I didn't follow it because I am trying to learn to shell program, and copy/pasting suggestions without understanding them at all is not much help to me, however perfect the code suggested. Thanks anyway though.

---------- Post updated at 11:28 AM ---------- Previous update was at 10:57 AM ----------

One more question then scottn, now i've looked at the code a bit more:

This line:
Code:
mkdir "${ALBUM/Album: /}"

Obviously makes a directory called

Code:
Elvis!

based on the string

Code:
Album: Elvis!


How does it do this? I would have thought sed or awk or something would be required to edit the line in some way, but there doesn't seem to be any utility present. Is this a function of mkdir?

Also, this doesn't work when the album name has a slash in it ie

Code:
mkdir: cannot create directory `Flaming Star/Wild in the Country/Follow That Dream': No such file or directory

Any ideas how to get around this?

Thanks.

Last edited by spoovy; 02-18-2011 at 08:31 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell command to convert low values to spaces

I neead a script which converts low values to the spaces, When I used sed -e 's/\x00/\x20/g' inputfile command it is removing the low values but not replacing it with spaces. Please help me. Its Uregent. Thanks Sam (12 Replies)
Discussion started by: bsreee35
12 Replies

2. AIX

How to append spaces to string values?

i/o file: abc,efg,xyz Required o/p file: "abc (Value + blank spaces=16) " ,"efg (Value +blank spaces=15) " ,"xyz (Value+ blank spaces =20) " In short input file value stores in result file with " i/p Value " added with spaces and are of fixed size like 16,15,20 How to do using... (2 Replies)
Discussion started by: AhmedLakadkutta
2 Replies

3. Shell Programming and Scripting

csh and variable values with spaces

my working shell is csh and even though if I try to run my script in plain sh, it behaves the same way. Here's a simple script: #!/bin/sh desc='"test my changes"' cmd="echo \"$desc\"" $cmd I want $desc to be passed as an argument to another command, but csh apparently doesn't like spaces in... (5 Replies)
Discussion started by: iskatel
5 Replies

4. UNIX for Dummies Questions & Answers

Get all values separated with spaces(solved)

Hi, i have this text: X (m) 4917536.9627 4917536.9673 0.0090 -0.0046 Y (m) -815726.1383 -815726.1294 0.0061 -0.0089 Z (m) 3965857.4730 3965857.4840 0.0071 -0.0110 X (m) 4917536.9627 4917537.1411 -0.1784 0.1710 Y (m) -815726.1383 -815726.4859 0.3476 0.3489 Z (m) 3965857.4730... (2 Replies)
Discussion started by: limadario
2 Replies

5. Shell Programming and Scripting

How to find spaces and update values on the same line.

Dear all, I'm trying to write a script where: A file contains more or less 2000 lines. To some of those lines, in a specific position, let's say 89-92 there are spaces. So, this script should find these spaces on specific position and update a value (from 2 to 1) to another position of the... (4 Replies)
Discussion started by: paniklas
4 Replies

6. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

7. Shell Programming and Scripting

adding spaces for a variable value

Hi, i have to form the header and add fillers(spaces) to it. I have done something like this. i have added 10 spaces at the end HDR="AAAABBBBCCNN " echo $HDR >> file1.dat but the spaces are not being stored in the file. How to add the spaces. (2 Replies)
Discussion started by: dnat
2 Replies

8. Shell Programming and Scripting

Variable containing spaces

Hi, my var is: PATH_LOG=/opt/WebSphere/CR Comune Roma.log a filename which contains blank chars. How can I call it from prompt ? Ex: ls $PATH_LOG or cat $PATH_LOG tks, Carmen- (2 Replies)
Discussion started by: Carmen123
2 Replies

9. Shell Programming and Scripting

Replace spaces with 0's having numeric values.

What could be the regular expression with gsub function in awk to replace all numerics having spaces before to be replaced with 0s? (1 Reply)
Discussion started by: videsh77
1 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question