简体   繁体   中英

az vim run-command invoke shell script with parameters

I want to run a script using az cli invoke.

I_PASS_THIS_VAR=${1}
az vm run-command invoke     --command-id RunShellScript \
                                 --scripts '@foo.sh' \
                                 --parameters=${I_PASS_THIS_VAR} \
                                 -o tsv

where I pass $I_PASS_THIS_VAR and it is some string. Then the foo.sh is:

#!/bin/bash
PARAM=${1}
if [ ${PARAM} ]
then
    echo "export MY_PARAM=${PARAM}" >> "${HOME_DIR}/.bashrc"
fi

I does not work. The MY_PARAM equals nothing. Why ?

To run the shell script in a Linux VM, the command should like this:

az vm run-command invoke -g group_name -n vm_name --command-id RunShellScript --scripts @run_command.sh --parameters param1

The shell script is in the place where you run the Azure CLI command. And the shell script for you will like this:

if [ $1 ]
then
        echo "export My_Param=$1" >> "/path/.bashrc"
fi

You can input the parameters in the Azure CLI command as the value of --parameter and quote them in the shell script like the above code. One point you should take care of is that the path in the above code should be an absolute path. For example, /home/useraccount/.bashrc and the useraccount is the user you want to export the environment variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM