Search code examples
azureazure-cliazure-cli2

Azure bash CLI support for while and do


I'm trying to write a bash script that parses arguments passed to the script:

while [[ $# -gt 0 ]]
do
        case $1 in
        -n|--name)
                VMNAME="$2"
                shift
                shift
        ;;
        -a|--admin-user)
                ADMINUSERNAME="$2"
                shift
                shift
        ;;
        -l|--location)
                LOCATION="$2"
                shift
                shift
        ;;
        -g|-resource-group)
                RESOURCEGROUPNAME="$2"
                shift
                shift
        ;;
        -a|--availability-set)
                AVAILABILITYSETNAME="$2"
                shift
                shift
        ;;
        *)    # unknown option
                echo unknwon "$1"
                shift # past argument
        ;;
        esac
done

echo "vn name = "
echo $VMNAME
echo "Admin User = " $ADMINUSERNAME
echo "Location = " $LOCATION
echo "Resource Group = " $RESOURCEGROUPNAME
echo "Availability Set = " $AVAILABILITYSETNAME

This works fine if I ssh into my Linux vm and run the script. If I try to execute the script from my Azure CLI I get the following errors:

myaccount@Azure:~/clouddrive$ bash test.bash -n test
test.bash: line 1: syntax error in conditional expression
'est.bash: line 1: syntax error near `]]
'est.bash: line 1: `while [[ $# -gt 0 ]]

How does the Azure CLI differ from the Linux bash shell in terms of it's programming language support? what kind of programming constructs and flow control is support in the Azure bash CLI?


Solution

  • I test in my lab and reproduce your error:

    enter image description here

    We should change Windows (CR LF) to Unix (LF), then upload to this shell.

    enter image description here