1

I have a following requirement of running the azure cli commands in a bash script file, for which i have created two script files as below

  1. to set the environment (environment.sh)

    #!/bin/sh
    echo "setting environment"
    export az_resource_group="test-group"
    echo "resource group is $az_resource_group"
    export az_source_server="test-server"
    echo "source_server is $az_source_server"
    export az_recovery_server="test-recovery-server"
    echo "recovery-server is$az_recovery_server"
    export az_restore_time="2020-07-27T12:52:21.000Z"
    echo "restore point in time is $az_restore_time"
    
  2. the actual script file to run azure cli commands (azurecommands.sh)

    #!/bin/sh
    resource_group=$az_resource_group
    recovery_server=$az_recovery_server
    source_server=$az_source_server
    restore_time=$az_restore_time
    echo "restoring postgresql database"
    echo "az postgres server restore -g $resource_group --restore-point-in-time $restore_time  -s $source_server -n $recovery_server" 
    

the above az command works properly when run individually or using arguments in bash, but if i use environment variables the command itself does not form correctly. so i decided to print it so that i can verify what is command that is being formed.

To my surprise the output is -n test-recovery-server 2020-07-27T12:52:21.000Z while my expected output is az postgres server restore -g test-group --restore-point-in-time -s test-server -n test-recovery-server .

I'm also making sure to run environment.sh file as follows:

source environment.sh

How can echo not print the correct command? is there something with the way i use environmental variables? if so please suggest me some other approach, because these commands need to be automated.

James Z
  • 12,209
  • 10
  • 24
  • 44
Urandoor Shilpa
  • 61
  • 1
  • 1
  • 9
  • 2
    Have you made sure there are no carriage returns in your environment variables? – oguz ismail Jul 27 '20 at 21:34
  • Instead with source, run the first script with `. ./environment.sh` and see if it helps. – Marko E Jul 27 '20 at 21:44
  • 1
    Using `xtrace` -- as in, `bash -x yourscript` -- is a good place to start. – Charles Duffy Jul 27 '20 at 22:04
  • By the way, `/bin/sh` is not bash. Tag your questions `sh`, not `bash`, if it's the shell you mean to ask about. (Even if your OS uses bash for `sh`, it runs in a different mode without all features available when started under that name; if you want all bash features to be available, use `#!/bin/bash`, not `#!/bin/sh`). – Charles Duffy Jul 27 '20 at 22:04
  • ...also, consider running your code through http://shellcheck.net/ and fixing what it finds. At least after you take out the `echo`, you'll have some bugs related to lack of quoting. (Part of why `set -x` is a better way to debug than `echo` is that it reflects your command arguments accurately -- `echo "first argument" "second argument"` looks exactly like `echo "first argument second argument"`, even though they're entirely different). – Charles Duffy Jul 27 '20 at 22:07
  • (And as Marko alludes to, `source` is one of the things that's guaranteed to work with `bash` but not with `sh`; with a baseline-POSIX version of `sh`, the guaranteed-available equivalent to `source` is the command named `.`). – Charles Duffy Jul 27 '20 at 22:08
  • What is the output of `azurecommands.sh | tr -d '\r' ` ? – Walter A Jul 27 '20 at 22:09
  • As you are echoing string literals that you don't see, this is what might be happening: `r=$'\rD E F'; echo "A B C $r"` (backspaces work too), you are overwriting the output. – Diego Torres Milano Jul 28 '20 at 00:13
  • I'm betting one or both of your files have DOS/WIndows line endings, which have a carriage return at the end of each line. This'll add the carriage returns to your variables, and cause great confusion. See ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – Gordon Davisson Jul 28 '20 at 00:59
  • add `.` to source the script. `. ./az.sh` – Bhaskar Tallamraju Jul 28 '20 at 03:55
  • Thank you all for the help . @GordonDavisson . This link helped me. There were DOS/Windows line endings. i converted them to unix endings using : https://support.nesi.org.nz/hc/en-gb/articles/218032857-Converting-from-Windows-style-to-UNIX-style-line-endings#:~:text=To%20write%20your%20file%20in,with%20UNIX%2Dstyle%20line%20endings. Appreciate the help :) – Urandoor Shilpa Jul 28 '20 at 06:45
  • If you solve the issue yourself please add an answer to show it. Maybe other communities are looking for it. – Charles Xu Jul 29 '20 at 02:15
  • Do you solve the problem? Or what do you want right now? – Charles Xu Jul 30 '20 at 08:07

0 Answers0