Solved

Help with bash script.

Posted on 1999-01-03
4
304 Views
Last Modified: 2013-12-26
If I use the program cda I can do a:
cda -dev /dev/cdrom on
cda -dev /dev/cdrom toc

and get the output:

Disc ID: rock 910f850b
Ulf Lundell / Maria Kom Tillbaka - Live - Cd 3

 01 05:59  Laglvs
 02 05:48  Evangeline
 03 04:41  Rom i regnet
 04 05:28  Ute pe vdgen igen
 05 06:30  Dansa nu
 06 04:58  Oh la la
 07 07:24  67
 08 03:17  Min vandrande vdn
 09 10:43  Isabella
 10 06:13  Leng vdg hem
 11 05:12  Maria kom tilllbaka

Total Time: 66:15

How can I make a script that take the trac-names and put them into variabels.
I mean that the variable track01 shall be laglvs and track11 Maria kom tillbaka.

How can I solve this in bash?
0
Comment
Question by:n94magho
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Accepted Solution

by:
hollandb earned 350 total points
ID: 1295098
I have created a bash script that uses one external program: 'expr', the rest is totally bash.  Note that the variables only exist for the process (child) - they are lost when the script ends hence the parent does not have access to the variables.

This script reads from ./track_titles.txt, but will read from standard input be default if the filename is removed.

It reads a whole line at a time and breaks it up into three variables (track_num, track_time, track_title) using the space as a separator.  If there are more than two spaces, the rest of the line is automatically put into the third variable, in this case 'track_title'.  It then checks to see if the length of the track_num is two(2) characters.  If it is not, it ignores the whole line and continues with the next line.  If it is, it assigns 'trackXX=<track title>' and exports the variable.  I have issued a set command at the end of the script to prove that variables are actually exported.

<START OF SCRIPT:>
#
# Allow input of track names and extract and print them.
#

while read track_num track_time track_title
do
        if [ `expr length "$track_num"` -eq 2 ]
        then
                echo $track_num $track_title;
                export track$track_num=$track_title
        fi
done < './track_titles.txt'

set
<END OF SCRIPT>
0
 

Author Comment

by:n94magho
ID: 1295099
Thanks!
0
 

Author Comment

by:n94magho
ID: 1295100
Hmmmmm.... one comment...

How can I print the trackXX variables so I ven se that they hold the right value.... It seems to me that soemthing is wrong.
0
 

Expert Comment

by:hollandb
ID: 1295101
I have played around with this a bit more and I think you will be happy with what I have come up with.
I have changed the script quite considerably and it doesn't use "expr", instead it uses a built in feature of bash to determine the length of a variable/array.  The code should be self-explanatory:

<START OF SCRIPT:>
#
# Allow input of track names and extract and print them.
#

# declare an array called 'titles':
declare -a titles;
declare -i index=1;

while read track_num track_time track_title
do
        # ${#VAR} uses the builtin capabilities of bash
        # to determine "the length of 'VAR'":
        if [ ${#track_num} -eq 2 ]
        then
                titles[$index]=$track_title;
                let index=$index+1;
        fi
done < './track_titles.txt'

# titles[@] means "the whole array";
# ${#titles[@]} means "the number of elements in the array".
# in this case, index = number of tracks.
index=${#titles[@]};

# print song titles backwards to prove they can be accessed
# by using the appropriate subscript:
while [ $index -gt 0 ]
do
        echo track$index=${titles[$index]};
        let index=$index-1;
done
<END OF SCRIPT>

Regards,
Brian.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question