LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 06-17-2015, 12:30 AM   #1
OutsiderFilms
LQ Newbie
 
Registered: May 2012
Posts: 10

Rep: Reputation: Disabled
Help with shell script


Hello everyone,

I wrote this basic shell script:

#!/bin/bash
for i in {1..15}
do
/usr/bin/avconv -y -i "/run/user/1000/gvfs/cdda:host=sr0/Track $i.wav" -acodec libmp3lame -b:a 320k -ac 2 -ar 44100 "/home/ofilms/Music/Converted/Luci/Track $i.mp3"
echo "Converting Track $i..."
done

It works.

I'm now trying to customise the script by having it ask me for initial and final track numbers. This is what I wrote:

#!/bin/bash

read -p "Enter number of first track : " t1
read -p "Enter number of last track : " tn

for i in {$t1.. $tn }
do
/usr/bin/avconv -y -i "/run/user/1000/gvfs/cdda:host=sr0/Track $i.wav" -acodec libmp3lame -b:a 320k -ac 2 -ar 44100 "/home/ofilms/Music/Converted/Luci/Track $i.mp3"
echo "Converting Track $i..."
done

This script asks for the first track number and stops. Please help me understand what I am doing wrong.

I'll extend this script to prompt for save locations and bit rates and sampling rates; in the final version it will read a text file with saved presets.


Thank you.
 
Old 06-17-2015, 12:38 AM   #2
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 820

Rep: Reputation: 197Reputation: 197
Change the default IFS from white spaces to "

Or else remove spaces in the prompt string.

OK
 
Old 06-17-2015, 12:49 AM   #3
OutsiderFilms
LQ Newbie
 
Registered: May 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks Ananta.

This is what I did (I've commented out the command in the script below and just left the echo for testing):

#!/bin/bash

IFS=''
read -p "Enter number of first track : " t1
read -p "Enter number of last track : " tn

for i in {$t1..$tn}
do
#/usr/bin/avconv -y -i "/run/user/1000/gvfs/cdda:host=sr0/Track $i.wav" -acodec libmp3lame -b:a 320k -ac 2 -ar 44100 "/home/ofilms/Music/Converted/Luci/Track $i.mp3"
echo "Converting Track"$i
done

Now it says:

Enter number of first track : 1
Enter number of last track : 16
Converting Track{1..16}

and stops.

Have I set the IFS correctly?
 
Old 06-17-2015, 12:54 AM   #4
OutsiderFilms
LQ Newbie
 
Registered: May 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Also,

I've tried variations of ($t1 .. $tn} { $t1 .. $tn } with and without whitespaces.

I get:

Converting Track1
Converting Track..
Converting Track14
Converting Track}
 
Old 06-17-2015, 12:58 AM   #5
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,845
Blog Entries: 36

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Please use bbcode code blocks. Regarding your range the format is likely only integers and environment variables can't be used to specify the range (pending reading the bash man page to be sure, which you should do to verify). Therefore, you should create a counter and use a while loop compound command instead. Use bash arithmetic to increase the counter. The loop should exit when the counter reaches the value $tn.

Last edited by sag47; 06-17-2015 at 01:00 AM.
 
Old 06-17-2015, 01:56 AM   #6
OutsiderFilms
LQ Newbie
 
Registered: May 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thank you sag47. That was spot on! The code that works is:

Code:
# mp3 converter using avconv

clear

read -p "Enter number of first track : " t1
read -p "Enter number of last track : " tn
echo "Converting tracks $t1 to "$tn

n=$t1
 
# convert tracks
while [ $n -le $tn ]
do
	/usr/bin/avconv -y -i "/run/user/1000/gvfs/cdda:host=sr0/Track $n.wav" -acodec libmp3lame -b:a 320k -ac 2 -ar 44100 "/home/ofilms/Music/Converted/Track $n.mp3"
	n=$(( n+1 ))
done

Now to read the bash manual and refine the script to read preset text files for conversion settings! Thank you.
 
Old 06-17-2015, 07:24 AM   #7
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,845
Blog Entries: 36

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
A couple of quick notes. You should use your env vars with ${t1} instead of $t1. By explicitly declaring the var you avoid mistakes like.

Code:
hello=friends
echo "$hello_again"
As opposed to

Code:
echo "${hello}_again"
You should also always quote your vars and arguments. Just a few tips on good bash habits.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script for run an shell script on server using ssh bloodstreetboy Linux - Server 5 01-12-2013 03:23 AM
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM


All times are GMT -5. The time now is 10:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration