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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
06-04-2009, 06:44 AM
|
#1
|
|
Member
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353
Rep:
|
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 :
But xine is not accepting the parameters.
HOWEVER, If I echo first:
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.
|
|
|
|
06-04-2009, 06:55 AM
|
#2
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Code:
awk 'BEGIN{ q="\047"}
{ lines[FNR]=$0 }
END{
srand()
RANDOM = int(1 + rand() * NR)
cmd = "xine "q lines[RANDOM] q
system(cmd)
}' file
|
|
|
|
06-04-2009, 07:09 AM
|
#3
|
|
Member
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353
Original Poster
Rep:
|
Quote:
Originally Posted by ghostdog74
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.
|
|
|
|
06-04-2009, 07:38 AM
|
#5
|
|
Member
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353
Original Poster
Rep:
|
Quote:
Originally Posted by soleilarw
|
Yes but, if I use:
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.
|
|
|
|
06-04-2009, 08:10 AM
|
#6
|
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,780
|
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.
|
|
|
|
06-04-2009, 08:16 AM
|
#7
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,690
|
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.
|
|
|
|
06-04-2009, 08:20 AM
|
#8
|
|
Member
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353
Original Poster
Rep:
|
Quote:
Originally Posted by Wim Sturkenboom
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.
|
|
|
|
06-04-2009, 08:24 AM
|
#9
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,690
|
Another thing that came to mind (not a reply to post #8):
Have you tried the eval command?
eval xine ${LINE}
|
|
|
|
06-04-2009, 08:27 AM
|
#10
|
|
Member
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353
Original Poster
Rep:
|
Quote:
Originally Posted by druuna
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.
|
|
|
|
06-04-2009, 08:30 AM
|
#11
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,690
|
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......
|
|
|
|
06-04-2009, 08:37 AM
|
#12
|
|
Member
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353
Original Poster
Rep:
|
Quote:
Originally Posted by druuna
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
|
|
|
|
06-04-2009, 08:39 AM
|
#13
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,690
|
Hi,
Did you yake a look at post #9?
You don't have to cut the original $LINE into pieces if that works.....
|
|
|
|
06-04-2009, 08:47 AM
|
#14
|
|
Member
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353
Original Poster
Rep:
|
Quote:
Originally Posted by druuna
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.
|
|
|
|
06-04-2009, 08:49 AM
|
#15
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,690
|
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}.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:04 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|