1

I need to write a script that plays a different sound every time it is ran. If you have an easier method then let me know. Right now the following script is what I have but I don't think the cat command is working like I want it to. Please assist.

#! /bin/sh
### BEGIN INIT INFO
# Provides:          rc.local
# Required-Start:    $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Run /etc/rc.local if it exist
### END INIT INFO

if [ "cat /home/lucio/Desktop/welcomevar" = "10" ];
then
echo 1 > /home/lucio/Desktop/welcomevar; mplayer         /home/lucio/Desktop/sounds/welcome1.mp3
fi

if [ "cat /home/lucio/Desktop/welcomevar" = "1" ];
then
echo 2 > /home/lucio/Desktop/welcomevar; mplayer     /home/lucio/Desktop/sounds/welcome2.mp3
fi

if [ "cat /home/lucio/Desktop/welcomevar" = "2" ];
then
echo 3 > /home/lucio/Desktop/welcomevar; mplayer /home/lucio/Desktop/sounds/welcome3.mp3
fi

if [ "cat /home/lucio/Desktop/welcomevar" = "3" ];
then
echo 4 > /home/lucio/Desktop/welcomevar; mplayer     /home/lucio/Desktop/sounds/welcome4.mp3
fi

if [ "cat /home/lucio/Desktop/welcomevar" = "4" ];
then
echo 5 > /home/lucio/Desktop/welcomevar; mplayer         /home/lucio/Desktop/sounds/welcome5.mp3
fi

if [ "cat /home/lucio/Desktop/welcomevar" = "5" ];
then
echo 6 > /home/lucio/Desktop/welcomevar; mplayer     /home/lucio/Desktop/sounds/welcome6.mp3
fi

if [ "cat /home/lucio/Desktop/welcomevar" = "6" ];
then
echo 7 > /home/lucio/Desktop/welcomevar; mplayer     /home/lucio/Desktop/sounds/welcome7.mp3
fi

if [ "cat /home/lucio/Desktop/welcomevar" = "7" ];
then
echo 8 > /home/lucio/Desktop/welcomevar; mplayer     /home/lucio/Desktop/sounds/welcome8.mp3
fi

if [ "cat /home/lucio/Desktop/welcomevar" = "8" ];
then
echo 9 > /home/lucio/Desktop/welcomevar; mplayer /home/lucio/Desktop/sounds/welcome9.mp3
fi

if [ "cat /home/lucio/Desktop/welcomevar" = "9" ];
then
echo 10 > /home/lucio/Desktop/welcomevar; mplayer            /home/lucio/Desktop/sounds/welcome10.mp3
fi

exit 0
Josh Lucio
  • 11
  • 1
  • See here for example: http://stackoverflow.com/questions/4227994/command-line-arguments-from-a-file-content – GhostCat Nov 26 '16 at 20:03

1 Answers1

0

See here for example: command line arguments from a file content ... but please note: your approach looks really really like some dirty hack.

You are repeating the same path all over the place; and you are even hardcoding the user name.

If you want your linux to play a different sound depending on some file; I think there much simpler methods.

For example, you could simply do something like

cd Desktop
ln -s sounds/welcomeX.mp3 welcome.mp3

The above will create a link from one of your mp3 files under the name of Desktop/welcome.mp3. Now you simply do

 mplayer /home/lucio/Desktop/welcome.mp3

and that will play the linked file. In other words: if you want to change the file to play, you just remove that link, and create another one. So instead of doing a super non-elegant mapping of a file content to file names ... just use the "same" file all the time, but use a link to point to actually different content.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248