Score:1

Pass an array to a bash function, syntax error: "(" unexpected

cg flag

I am trying to pass an array as an input for a different bash file. The main file is:

#!/bin/bash

arr=( "$@" )

path=`pwd`

cd ${arr[0]}

    read -p "Enter number of videous: " number_of_videous   

    for ((i=0;i<number_of_videous;i++))
    do
        read -p "Enter the resolution: " resolution
        
        if [ $resolution -eq 1080 ]
        then
            mkdir "${arr[1]}_1080_directory"
            ffmpeg -i ${arr[1]} -vf scale=1920:1080 -preset slow -crf 18 "${arr[1]}_1080_directory/output.mp4"
            arr=("${arr[1]}_1080_directory/output.mp4" "$resolution")
            sh "$path"/check_resolution_video.sh "${arr[@]}"
            .~/Script/bitrate.sh "${arr[1]}_1080_directory" "output.mp4"
            
        elif [ $resolution -eq 720 ]
            then
                mkdir "${arr[1]}_720_directory"
                ffmpeg -i ${arr[1]} -vf scale=1280:720 -preset slow -crf 18 "${arr[1]}_720_directory/output.mp4"
                arr=("${arr[1]}_720_directory/output.mp4" "$resolution")
                .~/Script/check_resolution_video.sh "${arr[@]}"     
                .~/Script/check_resolution_video.sh "${arr[@]}"
                .~/Script/bitrate.sh "${arr[1]}_720_directory" "output.mp4"
        fi      
    done

And the file that must be called and pass variables is:

#!/bin/bash

echo `pwd`

echo "$@"

arr=("$@")

resolution=`ffprobe -v error -select_streams v -show_entries stream=width,height -of csv=p=0:s=x "${arr[0]}"`

if [ ${arr[1]} -eq 1080 ] ; then    

    if [ $resolution == '1920x1080' ] ; then
        echo "The video: ${arr[1]} is true"
 
    fi

elif [ ${arr[1]} -eq 720 ] ; then       
    if [ $resolution == '1280x720' ]
    then
        echo "The video: ${arr[1]} is true"
    fi
fi

Everything is working until the "arr=("$@")" of the second file. Is not a permission issue.

The error is: "/home/alex/Script/check_resolution_video.sh: 7: Syntax error: "(" unexpected".

Moreover, the script receive the right parameters because I echo them first.

Do you have any idea what should I do in this situation?

hr flag
You're using `sh "$path"/check_resolution_video.sh "${arr[@]}"` - `sh` is not `bash` (at least, not by default)
waltinator avatar
it flag
Always paste your script into `https://shellcheck.net`, a syntax checker, or install `shellcheck` locally. Make using `shellcheck` part of your development process.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.