0

I need help with writing a bash script. I have very little knowledge on scripting, but am willing to learn.

I have a file with lines that looks like this: 1204200141345_song.mp3. The numbers are date and time like day=12, month=04, year=2014, hour=13, min=45

I need a script that will read through the file and if a line matches the current date and time it will play the mp3 via mplayer/mpg123 at the correct time. When the mp3 ends it must continue. The file changes all the time. New lines go in and other line are removed. The script will run as long as the pc is on.

I am not sure where to start, any advice is appreciated.

2
  • maybe when you shows at least minimal effort to solve the problem - you could get more answers... Welcome to SO - and read the help at the top... like: Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results... Commented Feb 23, 2014 at 14:36
  • Your filename is from the year 20014 Commented Feb 23, 2014 at 18:37

1 Answer 1

2

So, you willing to learn - some starting points...

The script will run as long as the pc is on.

Ok, this can be achieved with a loop what will run forever, like the next one:

while : do some_commands done 

When the mp3 ends it must continue.

As above - if the some_commands contains the command to playing the file - the script will wait until the command finishes and will start over - again, and again, and again... forever...

The file changes all the time. New lines go in and other line are removed.

Don't need to care until it is a file...

... if a line matches the current date and time it will play the mp3 via mplayer/mpg123 at the correct time.

So, first you need get the date in the needed format. This can be achieved with the date command. (man date)

The needed format is %d%m%Y%H%M, so the

date +"%d%m%Y%H%M" 

will print the current date/time in the wanted format

assing the result of the command to some variable in bash can be done with the

variable=$(command argumens) 

so,

datestr=(date +"%d%m%Y%H%M") echo $datestr 

will assing the current date/time to variable datestr.

... read through the file and if a line matches the current date and time ...

The Read-thru and match can be done with the grep command (man grep), so, you need use

grep "what_want_to_match" filename_where_want_to_match.txt 

The grep exits with a status codes

  • 0 - found a match
  • 1 - match not found
  • 2 - the file doesn't exists

The exist status of previous command is stored in the special variable $?. You can test the exit status, for example with the bash's case construction, like:

some_command case $? in 2) echo "The some_command exited with status 2" ; exit 2 ;; 1) echo "with status 1" ; do_something_else ;; 0) echo "normal exit" ;; esac 

Now, you have enough informations to try write the script yourself, and if you meet some specific error - ask again... ;)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.