LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with Bash scripts (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-bash-scripts-730629/)

greengrocer 06-04-2009 06:44 AM

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.

ghostdog74 06-04-2009 06:55 AM

Code:

awk 'BEGIN{ q="\047"}
{ lines[FNR]=$0 }
END{
    srand()
    RANDOM = int(1 + rand() * NR)
    cmd = "xine "q lines[RANDOM] q
    system(cmd)
}' file


greengrocer 06-04-2009 07:09 AM

Quote:

Originally Posted by ghostdog74 (Post 3562813)
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.

soleilarw 06-04-2009 07:23 AM

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

greengrocer 06-04-2009 07:38 AM

Quote:

Originally Posted by soleilarw (Post 3562840)
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}

Wim Sturkenboom 06-04-2009 08:10 AM

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?

druuna 06-04-2009 08:16 AM

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.

greengrocer 06-04-2009 08:20 AM

Quote:

Originally Posted by Wim Sturkenboom (Post 3562880)
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.

druuna 06-04-2009 08:24 AM

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

Have you tried the eval command?

eval xine ${LINE}

greengrocer 06-04-2009 08:27 AM

Quote:

Originally Posted by druuna (Post 3562888)
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.

druuna 06-04-2009 08:30 AM

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......

greengrocer 06-04-2009 08:37 AM

Quote:

Originally Posted by druuna (Post 3562913)
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

druuna 06-04-2009 08:39 AM

Hi,

Did you yake a look at post #9?

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

greengrocer 06-04-2009 08:47 AM

Quote:

Originally Posted by druuna (Post 3562924)
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.

druuna 06-04-2009 08:49 AM

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}.


All times are GMT -5. The time now is 10:40 PM.