Shell Script
How to convert a mp3 file to wav 8000htz 16bit mono(asterisk format)
- wget http://ffmpeg.org/releases/ffmpeg-3.0.tar.bz2
- tar -xvf ffmpeg-3.0.tar.bz2
- cd ffmpeg-3.0
- ./configure --disable-yasm
- make
- make install
- ffmpeg -i <source.mp3> -ar 8000 -ac 1 <destination.wav>
To execute a shell files u need to convert that file into execute mode by the following command.
chmod 777 filename.sh
==============================================================================
To execute the file in the terminal type this.
./filename.sh
==============================================================================
The following example is the process to create a shell script file and execute at a particular interval. The shell script will move a particular directory contains some sound file of the previous day in the backup path to increase disk space. The shell script will execute every day at 1 AM.
Open vi editor by the following command
vi /home/movefile.sh
Type this in the VI editor.
#/bin/bash
a=`date -d yesterday +%d%m%y`
echo "Yesterday is $a"
pdir="/home/sandip/$a"
echo $pdir
mv $pdir /home/sandip/temp
exit 0
Press :wq! to save the file.
Now type chmod 777 movefile.sh to make it executable.
Process to execute the job at a specific interval (daily at 1:00 am). Edit crontab at put the following
crontab -e
Put the following entries
0 1 * * * /home/movefile.sh
Save the file and type crontab -l to check the crontab list
=====================================================================================
What is crontab?
The cron daemon provides the ability for a system administrator or any other user (if permitted to do so) to automate the routine running of scripts, tasks or any other server function on a regular basis. The cron daemon is somewhat similar to what the "scheduled tasks" tool on a Windows machine would be.
crontab -e - opens the user's crontab file for viewing/editing
crontab -l - simply lists the crontab file's contents for the user. Think of it as a "cat" function for the crontab.
crontab -r - removes the crontab file contents for the user
A typical crontab entry might look like this:
30 0,12 * * * /usr/local/scripts/whatever.script
Crontab is split up into 5 sections. The following chart illustrates what each section of the schedule is for:
1. Minute - Minutes after the hour (0-59).
2. Hour - 24-hour format (0-23).
3. Day - Day of the month (1-31).
4. Month - Month of the year (1-12).
5. Weekday - Day of the week. (0-6, where 0 indicates Sunday).
What if we had something like this:
15,45 0,12,6 20 1,2,3 0 /some/script/or/command
It is a pretty complex crontab entry. Let's Understand it..
15,45 - This means that the task will run at 15 and 45 minutes past the hour. But what hours and what days? Well, that's coming up.
0.12.6 - The task will run during the hours of 0 (midnight), 12 (noon) and 6AM.
20 - The task will run on the 20th day of the month. But during what months?
1,2,3 - The task will run only during the months of January, February and March.
0 - The task will only run on a Sunday.
So, when we put all of this information together, here is what it boils down to:
This task is going to run at 0:15, 0:45, 6:15, 6:45, 12:15 and 12:45 on the 20th of January, February and March IF that day falls on a Sunday. Pretty cool, huh?
The following Shell Script is for downloading files fro a particular ftp server.
===================================================================================
#!/bin/bash
con='Y'
while test $con=='Y'
do
HOST='10.201.1.114'
USER='sandip'
PASSWD='linux'
FILE='*.ini'
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
cd DATA
mget $FILE
bye
END_SCRIPT
sleep 5
done
==================================================================================
The Following Shell Script is to convert any particular MP3 or WAV file 8000htz 16 bit mono format. To run this app properly you need to install the mpg123 and sox to your Linux box.
==================================================================================
#!/bin/bash
con='Y'
while test $con!='N'
do
#if [ -e /TEMPAUDIO/TempFinal/* ]; then
for x in /TEMPAUDIO/TempFinal/*
do
#echo $x
dirname=`expr substr $x 22 6`
#echo $dirname
# mkdir /data/sounds/$dirname
filename=`expr substr $x 29 9`
#echo $filename
filexten=`expr substr $filename 7 3`
#echo $filexten
if [ $filexten = 'wav' ]; then
sox $x -r 8000 -c 1 -w -s /TEMPAUDIO/$dirname/$filename
fi
if [ $filexten = 'mp3' ]; then
rfilename=`expr substr $filename 1 5`
rawfilename=$rfilename'.raw'
finalwavefile=$rfilename'.wav'
#echo $rfilename
#echo $rawfilename
#echo $finalwavefile
#echo /TEMPAUDIO/$dirname/$finalwavefile
mpg123 -s --rate 44100 --mono $x > /TEMPAUDIO/Temp/RAW/$rawfilename
sox -r 44100 -w -s -c 1 /TEMPAUDIO/Temp/RAW/$rawfilename -r 8000 -c 1 -w /TEMPAUDIO/$dirname/$finalwavefile
rm /TEMPAUDIO/Temp/RAW/$rawfilename
fi
mv $x /TEMPAUDIO/Temp
done
#fi
sleep 1
done
===================================================================================