shell - Too many arguments error in bash script -


i have modified script download songs youtube getting following error when run this:

sh youtube2mp3.sh https://www.youtube.com/watch?v=gpoj6iu8fqq 

errors:

youtube2mp3.sh: line 31: [: many arguments youtube2mp3.sh: line 39: [: many arguments youtube2mp3.sh: line 49: [: many arguments sorry system encountered problem. 

the line numbers referring 3 if [ -f $video_title.$ext1 ] lines ... thought had arguments ok worked in previous version, i'm stuck @ point — explain need correct it?

address=$1  video_title="$(python youtube-dl $address)"  ext1="flv" ext2="mp4" ext3="webm"    if [ -f  $video_title.$ext1 ]     ffmpeg -i $video_title.$ext1 "$video_title".wav     lame "$video_title".wav "$video_title".mp3     rm $video_title.$ext1 "$video_title".wav else     if [ -f $video_title.$ext2 ]             ffmpeg -i $video_title.$ext2 "$video_title".wav         lame "$video_title".wav "$video_title".mp3         rm $video_title.$ext2 "$video_title".wav     else             if [ -f $video_title.$ext3 ]                     ffmpeg -i $video_title.$ext3 -acodec libmp3lame -aq 4 "$video_title".mp3             rm $video_title.$ext3         else             echo "sorry system encountered problem."          fi      fi fi 

whenever have shell script need debug, use set -xv. turn on verbose mode print out each line executed, , turn on xtrace display command when expansion done.

you can turn off set -xv set +xv. can envelope entire script, or lines causing heartaches.

if did this, think you'll see $video_title gets expanded names spaces in them, , that's when errors. should putting quotes everywhere in script have `$video_title":

if [ -f "$video_title".$ext2 ]   #quotes!     ffmpeg -i "$video_title".$ext2 "$video_title".wav  #even more quotes 

remember [ command , synonym test command. if command written as:

if test -f "$video_title".$ext2  #quotes! 

like commands, shell break parameters give command on white spaces. thus, title "the life of radish" broken 5 separate parameters "the", "life", "of", "a", , "radish" before being passed test command.

this explains error message:

youtube2mp3.sh: line 31: [: many arguments 

because -f command line parameter can take 1 additional parameter , not 5 parameters shell passed it. quotes keep shell breaking video title separate parameters -f flag.

by way, print out manpage on test ($ man test) , you'll see takes of same parameters [ ... ] take. explains why [ , ] need surrounded spaces -- these unix commands , unix commands must surrounded white spaces.

also run command:

$ ls -il /bin/[ /bin/test 10958 -rwxr-xr-x  2 root  wheel  18576 may 28 22:27 /bin/[ 10958 -rwxr-xr-x  2 root  wheel  18576 may 28 22:27 /bin/test 

that first parameter inode. it's sort of real name of file (what think file name , directory attributes of inode). see both test , [ have same inode number, , same file linked (via ln command) same file.

(not entirely true. [ builtin command both korn , bash using. however, [ builtin command internally linked builtin command called test anyway.)


Comments