Too many arguments in a bash script

I changed this script to download songs from YouTube, but when I start, I get the following error:

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

Error:

 youtube2mp3.sh: line 31: [: too many arguments youtube2mp3.sh: line 39: [: too many arguments youtube2mp3.sh: line 49: [: too many arguments Sorry but the system encountered a problem. 

Line numbers refer to three if [ -f $video_title.$ext1 ] lines ... I thought I had arguments in order since it worked in the previous version, but I was stuck at this point - can someone explain What do I need to do to fix?

 address=$1 video_title="$(python youtube-dl $address)" ext1="flv" ext2="mp4" ext3="webm" if [ -f $video_title.$ext1 ] then 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 ] then 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 ] then ffmpeg -i $video_title.$ext3 -acodec libmp3lame -aq 4 "$video_title".mp3 rm $video_title.$ext3 else echo "Sorry but the system encountered a problem." fi fi fi 
+6
source share
2 answers

Whenever you have a shell script you need to debug, use set -xv . This will enable verbose mode, which will print each line executed and enable xxtrace, which will display the command when the extension is executed.

You can disable set -xv with set +xv . You can convert your entire script, or just the lines that cause your suffering.

If you did this, I think you'll see that $video_title expands to names with spaces in them, and when you get your errors. You should put quotation marks everywhere in your script where you have `$ video_title":

 if [ -f "$video_title".$ext2 ] #QUOTES! then ffmpeg -i "$video_title".$ext2 "$video_title".wav #EVEN MORE QUOTES 

Remember that [ is actually a command and is synonymous with the test command. Your if command can be written as:

 if test -f "$video_title".$ext2 #QUOTES! then 

Like all commands, the shell breaks the parameters that you pass to the command into spaces. Thus, your name, Radish Life, will be broken down as five separate parameters, The, Life, of, a, and Radish, before passing test to this command.

This explains your error message:

 youtube2mp3.sh: line 31: [: too many arguments 

Since the -f command line parameter can accept only one additional parameter, and not five parameters passed to it by the shell. Quotation marks do not allow the shell to break your video title into separate parameters with the -f flag.

By the way, print the control page in the test ( $ man test ), and you will see that it accepts all the same parameters as your [ ... ] . It also explains why [ and ] should be surrounded by spaces - these are Unix commands, and Unix commands should be surrounded by spaces.

Also run the following 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 

This first parameter is an inode. This is similar to the real file name (as you think, this is the file name, and the directory is the inode attributes). You will see that both test and [ have the same inode number and thus the really same file that is linked (via the ln command) to the same file.

(Not quite true. [ Is the built-in command for both Korn and BASH, which you probably use. However, the built-in command [ internally linked to another built-in command called test .)

+6
source

Always specify a parameter extension. The value of $video_title split into a few words, which confuses the [ .

 if [ -f "$video_title.$ext1" ] then ffmpeg -i "$video_title.$ext1" ... 
+8
source

Source: https://habr.com/ru/post/949460/


All Articles