LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 06-04-2009, 06:44 AM   #1
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Rep: Reputation: 37
Help with Bash scripts


Hello all,

I have written a bash script which counts the number of lines in a given text file, and then chooses a random number and then reads the line number from the file basis that random number. The script looks like this:

Code:
#!/bin/bash

FILE="myfile"
NUM=$(wc -l < ${FILE})
RND=$((RANDOM % $NUM+1))
LINE=$(sed -n ${RND}p ${FILE})
Lets say the content of each line in myfile was a bunch of parameters or options for Xine. And lets say that I want my script to launch Xine using the contents of the line that was read by sed. How would I finally use xine in my script?

I tried adding as the last line in my script :

Code:
xine ${LINE}
But xine is not accepting the parameters.

HOWEVER, If I echo first:

Code:
echo xine ${LINE}
then I can copy and paste the line that was echoed into the terminal window and Xine executes properly using the correct switches.

I really hope someone can help.

Last edited by greengrocer; 06-04-2009 at 07:46 AM.
 
Old 06-04-2009, 06:55 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
awk 'BEGIN{ q="\047"}
{ lines[FNR]=$0 }
END{
    srand()
    RANDOM = int(1 + rand() * NR)
    cmd = "xine "q lines[RANDOM] q
    system(cmd)
}' file
 
Old 06-04-2009, 07:09 AM   #3
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by ghostdog74 View Post
Code:
awk 'BEGIN{ q="\047"}
{ lines[FNR]=$0 }
END{
    srand()
    RANDOM = int(1 + rand() * NR)
    cmd = "xine "q lines[RANDOM] q
    system(cmd)
}' file
Hmmm, doesn't seem to work.

It causes Xine to throw up an error box saying it cannot find the MRL.

Plus, I don't quite understand all of what you have presented there.
 
Old 06-04-2009, 07:23 AM   #4
soleilarw
Member
 
Registered: Apr 2009
Posts: 107

Rep: Reputation: 19
See http://www.linuxquestions.org/questi...l.....-309312/ for a similar problem. Basically it is possible that the MRL's that are being read in are not valid or they are no full pathnames.

Linux Archive

Last edited by soleilarw; 06-18-2009 at 04:23 AM.
 
Old 06-04-2009, 07:38 AM   #5
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by soleilarw View Post
See http://www.linuxquestions.org/questi...l.....-309312/ for a similar problem. Basically it is possible that the MRL's that are being read in are not valid or they are no full pathnames.
Yes but, if I use:

Code:
echo xine ${LINE}
then I can copy and paste the line that was echoed into the terminal window and Xine executes properly using the correct switches.

So the MRL is valid, I just don't know how to make a BASH script launch xine and pass a bunch of parameters that are stored in a $variable.

An example of the parameters would be:

--enqueue -S session=0,mrl=/home/user/kelly.mpg#volume:100

Now if I was to type into a terminal console:

xine --enqueue -S session=0,mrl=/home/user/kelly.mpg#volume:100

it would work.

I also know that when I run my BASH script, $LINE actually does look like: --enqueue -S session=0,mrl=/home/user/kelly.mpg#volume:100

but for some reason, if I try to use:

xine ${LINE}

The contents of $LINE are not treated as one bunch of parameters. I have a funny feeling that xine is trying to treat $LINE as just an MRL, which $LINE is more than just an MRL.


I have also tried (with $LINE = /home/user/kelly.mpg#volume:100 (hence, just the valid url with a volume setting on the end which is allowed when using the enqueue option)

xine --enqueue -S session=0,mrl=${LINE}

Last edited by greengrocer; 06-04-2009 at 07:44 AM.
 
Old 06-04-2009, 08:10 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Code:
LINE="--enqueue -S session=0,mrl=/usr/share/apps/k3b/extra/k3bphotosvcd.mpg#volume:100"
xine $LINE
Works. I'm not a bash-scripter so what do the curlies do?

Last edited by Wim Sturkenboom; 06-04-2009 at 08:12 AM.
 
Old 06-04-2009, 08:16 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

What happens if you try the following from the command line:
Code:
LINE="--enqueue -S session=0,mrl=/home/user/kelly.mpg#volume:100"
echo ${LINE}
xine ${LINE}
If that doesn't work, try adding a set -x between the echo and xine statement, that should show you what bash does and how xine is executed.
 
Old 06-04-2009, 08:20 AM   #8
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by Wim Sturkenboom View Post
Code:
LINE="--enqueue -S session=0,mrl=/usr/share/apps/k3b/extra/k3bphotosvcd.mpg#volume:100"
xine $LINE
Works. I'm not a bash-scripter so what do the curlies do?
I've discovered that:

Code:
xine "$LINE"#volume:10
will work, so long as the line from the file looked like this:

/home/user/kelly.mpg

which is kind of nearly what I want, only that:

the line from the file MUST have a volume specifier at the end, so the line from the file has to look like this:

/home/user/kelly.mpg#volume:10

So my next question would be, if I use sed to get read the line and print the line to $LINE. How could I strip the #volume:10 from it and make $VOL = #Volume:10 ?

Gosh I hope this has made sense to someone, coz its difficult to describe.
 
Old 06-04-2009, 08:24 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Another thing that came to mind (not a reply to post #8):

Have you tried the eval command?

eval xine ${LINE}
 
Old 06-04-2009, 08:27 AM   #10
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by druuna View Post
Hi,

What happens if you try the following from the command line:
Code:
LINE="--enqueue -S session=0,mrl=/home/user/kelly.mpg#volume:100"
echo ${LINE}
xine ${LINE}
If that doesn't work, try adding a set -x between the echo and xine statement, that should show you what bash does and how xine is executed.
Well, that throws some light onto whats going wrong (I could see that #volume:10 was being treated as part of the MRL and also adding a ' to where ever there was a space in a file name). Soooo, basically if I use:

xine "$LINE" seems to work with everything including file names that have spaces, so long as I dont have the #volume:10 on the end of the line.

However, I need the volume argument on the end of the line, and I need to pass that as an arg to xine (some files are louder than others you see). So if I can just know how to separate the two pieces of information into two separate variables, I think it might do the trick.

So close to what I need, I can smell it.

Last edited by greengrocer; 06-04-2009 at 08:31 AM.
 
Old 06-04-2009, 08:30 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

The # in the options line could be (is??) seen by bash as special when using ${LINE} (man bash for the details). That is why $LINE works (more or less) and ${LINE} does not.

Maybe my eval option helps......
 
Old 06-04-2009, 08:37 AM   #12
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by druuna View Post
Hi,

The # in the options line could be (is??) seen by bash as special when using ${LINE} (man bash for the details). That is why $LINE works (more or less) and ${LINE} does not.

Maybe my eval option helps......
Yeah cool, I also think that if I read a line from a file that looks like this:

/home/user/kelly.mpg#volume:100

and if I can use sed to put the above line into a variable called $TEMP

and then somehow split $TEMP into two pieces, so that:

$LINE = /home/user/kelly.mpg

and introduce a new variable for volume, so that:

$VOL = #volume:10

I reckon I can make that work by doing:

xine --enqueue "$LINE"$VOL
 
Old 06-04-2009, 08:39 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Did you yake a look at post #9?

You don't have to cut the original $LINE into pieces if that works.....
 
Old 06-04-2009, 08:47 AM   #14
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by druuna View Post
Hi,

Did you yake a look at post #9?

You don't have to cut the original $LINE into pieces if that works.....
Hi,

If the line in the file looks like this:

/home/user/kelly.mpg#volume:100

Using either ${LINE} or $LINE behaves the same.

It seems the #volume:100 really needs to be cut from the $LINE and put into another variable like $VOL.


I just tested:

Code:
$LINE="/home/user/kelly.mpg"
$VOL="#volume:100"
xine "$LINE"$VOL
and it worked like a dream.

So now I need to learn how to cut the line into two pieces.


Edit: Oh wait, eval may work....one sec.

Last edited by greengrocer; 06-04-2009 at 08:50 AM.
 
Old 06-04-2009, 08:49 AM   #15
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
It is still not clear from your answer if you tried the eval command

Code:
LINE="--enqueue -S session=0,mrl=/home/user/kelly.mpg#volume:100"
eval xine $LINE
Also try ${LINE}.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripts grishaoks Linux - Newbie 4 12-05-2008 07:14 AM
I need some help with BASH scripts. BoB4ik Programming 10 12-20-2007 04:06 PM
bash scripts hoffmanyew Programming 3 08-11-2005 01:27 AM
Need Help With Bash Scripts the_woelf Linux - Software 4 06-30-2004 09:09 AM
$? in Bash scripts clinton Linux - Newbie 4 02-20-2004 11:15 AM


All times are GMT -5. The time now is 01:17 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration