bash scripting problem

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












Ubuntu 18.04 server + LXDE. I can't get the hang of this bash scripting.

This is my script "start" (with line numbering):



#!/bin/bash
reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
if $reply == 0
then
echo "NFS mounted OK"
else
echo "Mounting NFS failed: $reply"
fi

reply=sudo mount LABEL="60GB" /home/dk/60GB
if $reply == 0
then
echo "60GB mounted OK"
else
echo "Mounting 60GB failed: $reply"
fi
exit 0


This is the result of running it:



$ ./scripts/start
mount: only root can do that
./start: line 3: ==: command not found
Mounting NFS failed:
mount: only root can do that
./start: line 11: ==: command not found
Mounting 60GB failed:
$


  • Why did it ignore the sudo before mount?

  • Why did it not consider if to be a command ?

  • Why did it not print the (non-zero) value of $reply?









share|improve this question



























    up vote
    1
    down vote

    favorite












    Ubuntu 18.04 server + LXDE. I can't get the hang of this bash scripting.

    This is my script "start" (with line numbering):



    #!/bin/bash
    reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
    if $reply == 0
    then
    echo "NFS mounted OK"
    else
    echo "Mounting NFS failed: $reply"
    fi

    reply=sudo mount LABEL="60GB" /home/dk/60GB
    if $reply == 0
    then
    echo "60GB mounted OK"
    else
    echo "Mounting 60GB failed: $reply"
    fi
    exit 0


    This is the result of running it:



    $ ./scripts/start
    mount: only root can do that
    ./start: line 3: ==: command not found
    Mounting NFS failed:
    mount: only root can do that
    ./start: line 11: ==: command not found
    Mounting 60GB failed:
    $


    • Why did it ignore the sudo before mount?

    • Why did it not consider if to be a command ?

    • Why did it not print the (non-zero) value of $reply?









    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Ubuntu 18.04 server + LXDE. I can't get the hang of this bash scripting.

      This is my script "start" (with line numbering):



      #!/bin/bash
      reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
      if $reply == 0
      then
      echo "NFS mounted OK"
      else
      echo "Mounting NFS failed: $reply"
      fi

      reply=sudo mount LABEL="60GB" /home/dk/60GB
      if $reply == 0
      then
      echo "60GB mounted OK"
      else
      echo "Mounting 60GB failed: $reply"
      fi
      exit 0


      This is the result of running it:



      $ ./scripts/start
      mount: only root can do that
      ./start: line 3: ==: command not found
      Mounting NFS failed:
      mount: only root can do that
      ./start: line 11: ==: command not found
      Mounting 60GB failed:
      $


      • Why did it ignore the sudo before mount?

      • Why did it not consider if to be a command ?

      • Why did it not print the (non-zero) value of $reply?









      share|improve this question















      Ubuntu 18.04 server + LXDE. I can't get the hang of this bash scripting.

      This is my script "start" (with line numbering):



      #!/bin/bash
      reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS
      if $reply == 0
      then
      echo "NFS mounted OK"
      else
      echo "Mounting NFS failed: $reply"
      fi

      reply=sudo mount LABEL="60GB" /home/dk/60GB
      if $reply == 0
      then
      echo "60GB mounted OK"
      else
      echo "Mounting 60GB failed: $reply"
      fi
      exit 0


      This is the result of running it:



      $ ./scripts/start
      mount: only root can do that
      ./start: line 3: ==: command not found
      Mounting NFS failed:
      mount: only root can do that
      ./start: line 11: ==: command not found
      Mounting 60GB failed:
      $


      • Why did it ignore the sudo before mount?

      • Why did it not consider if to be a command ?

      • Why did it not print the (non-zero) value of $reply?






      bash






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 7 mins ago









      muru

      132k19280478




      132k19280478










      asked 30 mins ago









      Dave Kimble

      92




      92




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          Well you're assigning output of a command incorrectly. Should be:



           reply=$(sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS)


          That's called command substitution. Sometimes you see it in old Bourne shell syntax with backquotes instead of $(...), but that's not recommended nowadays because backquotes can't be nested easily, while you can do nested $( cmd1 $(cmd2))




          Why did it not consider "if" to be a command ?




          If statements operate on commands, but what you give is variable $reply, which only exists temporarily ( see later in the answer about that) so it exists only for mount comma d's environment, but shell running the script knows nothing of it - it doesn't exist. So compare strings use [, and yes it's a command also known as test. So do



          if test "$reply" == 0


          See quotes ? That's important, otherwise if there's spaces, shell will expand variable to two or more separate entities not one.



          But if you want to check exit status of command, just do



          if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS


          See no sudo ? That's because it's usually redundant to use sudo in script multiple times. Just call script itself with sudo and all commands will have root privilege.



          I'd also say echo is redundant because mount command already can print errors to terminal, but if you want custom message, you can do



          if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS 1> /dev/null
          then
          echo "Success"
          else
          echo "Fail"
          fi


          Because of 1> normal output suppressed ( actually redirected to /dev/null especially for that purpose) but errors will show if anything goes wrong.




          Why did it ignore the "sudo" before "mount" ?




          As for why you're getting error message that's because the form



          reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS


          is treated as envvariable=value command arg1 arg2, i.e. you're putting variable result with value sudo into environment of mount command. The command will know about it, but after command exits - the variable is gone too.




          Why did it not print the (non-zero) value of $reply ?




          Because reply doesn't store exit status as explained before. There's actually a variable for that provided by shell already, $? so use that as



          if [ $? -eq 0 ] 


          Rinse and repeat the suggestions for other statements in your script.



          Good luck






          share|improve this answer






















          • @muru Ah, right, I rushed on that one. Yep , pre-command assignments are only valid for that command's execution environment
            – Sergiy Kolodyazhnyy
            7 mins ago










          • @muru Edited. Thanks for the catch
            – Sergiy Kolodyazhnyy
            4 mins ago










          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "89"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "",
          contentPolicyHtml: "",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1088748%2fbash-scripting-problem%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote













          Well you're assigning output of a command incorrectly. Should be:



           reply=$(sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS)


          That's called command substitution. Sometimes you see it in old Bourne shell syntax with backquotes instead of $(...), but that's not recommended nowadays because backquotes can't be nested easily, while you can do nested $( cmd1 $(cmd2))




          Why did it not consider "if" to be a command ?




          If statements operate on commands, but what you give is variable $reply, which only exists temporarily ( see later in the answer about that) so it exists only for mount comma d's environment, but shell running the script knows nothing of it - it doesn't exist. So compare strings use [, and yes it's a command also known as test. So do



          if test "$reply" == 0


          See quotes ? That's important, otherwise if there's spaces, shell will expand variable to two or more separate entities not one.



          But if you want to check exit status of command, just do



          if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS


          See no sudo ? That's because it's usually redundant to use sudo in script multiple times. Just call script itself with sudo and all commands will have root privilege.



          I'd also say echo is redundant because mount command already can print errors to terminal, but if you want custom message, you can do



          if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS 1> /dev/null
          then
          echo "Success"
          else
          echo "Fail"
          fi


          Because of 1> normal output suppressed ( actually redirected to /dev/null especially for that purpose) but errors will show if anything goes wrong.




          Why did it ignore the "sudo" before "mount" ?




          As for why you're getting error message that's because the form



          reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS


          is treated as envvariable=value command arg1 arg2, i.e. you're putting variable result with value sudo into environment of mount command. The command will know about it, but after command exits - the variable is gone too.




          Why did it not print the (non-zero) value of $reply ?




          Because reply doesn't store exit status as explained before. There's actually a variable for that provided by shell already, $? so use that as



          if [ $? -eq 0 ] 


          Rinse and repeat the suggestions for other statements in your script.



          Good luck






          share|improve this answer






















          • @muru Ah, right, I rushed on that one. Yep , pre-command assignments are only valid for that command's execution environment
            – Sergiy Kolodyazhnyy
            7 mins ago










          • @muru Edited. Thanks for the catch
            – Sergiy Kolodyazhnyy
            4 mins ago














          up vote
          3
          down vote













          Well you're assigning output of a command incorrectly. Should be:



           reply=$(sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS)


          That's called command substitution. Sometimes you see it in old Bourne shell syntax with backquotes instead of $(...), but that's not recommended nowadays because backquotes can't be nested easily, while you can do nested $( cmd1 $(cmd2))




          Why did it not consider "if" to be a command ?




          If statements operate on commands, but what you give is variable $reply, which only exists temporarily ( see later in the answer about that) so it exists only for mount comma d's environment, but shell running the script knows nothing of it - it doesn't exist. So compare strings use [, and yes it's a command also known as test. So do



          if test "$reply" == 0


          See quotes ? That's important, otherwise if there's spaces, shell will expand variable to two or more separate entities not one.



          But if you want to check exit status of command, just do



          if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS


          See no sudo ? That's because it's usually redundant to use sudo in script multiple times. Just call script itself with sudo and all commands will have root privilege.



          I'd also say echo is redundant because mount command already can print errors to terminal, but if you want custom message, you can do



          if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS 1> /dev/null
          then
          echo "Success"
          else
          echo "Fail"
          fi


          Because of 1> normal output suppressed ( actually redirected to /dev/null especially for that purpose) but errors will show if anything goes wrong.




          Why did it ignore the "sudo" before "mount" ?




          As for why you're getting error message that's because the form



          reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS


          is treated as envvariable=value command arg1 arg2, i.e. you're putting variable result with value sudo into environment of mount command. The command will know about it, but after command exits - the variable is gone too.




          Why did it not print the (non-zero) value of $reply ?




          Because reply doesn't store exit status as explained before. There's actually a variable for that provided by shell already, $? so use that as



          if [ $? -eq 0 ] 


          Rinse and repeat the suggestions for other statements in your script.



          Good luck






          share|improve this answer






















          • @muru Ah, right, I rushed on that one. Yep , pre-command assignments are only valid for that command's execution environment
            – Sergiy Kolodyazhnyy
            7 mins ago










          • @muru Edited. Thanks for the catch
            – Sergiy Kolodyazhnyy
            4 mins ago












          up vote
          3
          down vote










          up vote
          3
          down vote









          Well you're assigning output of a command incorrectly. Should be:



           reply=$(sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS)


          That's called command substitution. Sometimes you see it in old Bourne shell syntax with backquotes instead of $(...), but that's not recommended nowadays because backquotes can't be nested easily, while you can do nested $( cmd1 $(cmd2))




          Why did it not consider "if" to be a command ?




          If statements operate on commands, but what you give is variable $reply, which only exists temporarily ( see later in the answer about that) so it exists only for mount comma d's environment, but shell running the script knows nothing of it - it doesn't exist. So compare strings use [, and yes it's a command also known as test. So do



          if test "$reply" == 0


          See quotes ? That's important, otherwise if there's spaces, shell will expand variable to two or more separate entities not one.



          But if you want to check exit status of command, just do



          if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS


          See no sudo ? That's because it's usually redundant to use sudo in script multiple times. Just call script itself with sudo and all commands will have root privilege.



          I'd also say echo is redundant because mount command already can print errors to terminal, but if you want custom message, you can do



          if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS 1> /dev/null
          then
          echo "Success"
          else
          echo "Fail"
          fi


          Because of 1> normal output suppressed ( actually redirected to /dev/null especially for that purpose) but errors will show if anything goes wrong.




          Why did it ignore the "sudo" before "mount" ?




          As for why you're getting error message that's because the form



          reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS


          is treated as envvariable=value command arg1 arg2, i.e. you're putting variable result with value sudo into environment of mount command. The command will know about it, but after command exits - the variable is gone too.




          Why did it not print the (non-zero) value of $reply ?




          Because reply doesn't store exit status as explained before. There's actually a variable for that provided by shell already, $? so use that as



          if [ $? -eq 0 ] 


          Rinse and repeat the suggestions for other statements in your script.



          Good luck






          share|improve this answer














          Well you're assigning output of a command incorrectly. Should be:



           reply=$(sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS)


          That's called command substitution. Sometimes you see it in old Bourne shell syntax with backquotes instead of $(...), but that's not recommended nowadays because backquotes can't be nested easily, while you can do nested $( cmd1 $(cmd2))




          Why did it not consider "if" to be a command ?




          If statements operate on commands, but what you give is variable $reply, which only exists temporarily ( see later in the answer about that) so it exists only for mount comma d's environment, but shell running the script knows nothing of it - it doesn't exist. So compare strings use [, and yes it's a command also known as test. So do



          if test "$reply" == 0


          See quotes ? That's important, otherwise if there's spaces, shell will expand variable to two or more separate entities not one.



          But if you want to check exit status of command, just do



          if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS


          See no sudo ? That's because it's usually redundant to use sudo in script multiple times. Just call script itself with sudo and all commands will have root privilege.



          I'd also say echo is redundant because mount command already can print errors to terminal, but if you want custom message, you can do



          if mount 192.168.0.2:/home/dk/NFS /home/dk/NFS 1> /dev/null
          then
          echo "Success"
          else
          echo "Fail"
          fi


          Because of 1> normal output suppressed ( actually redirected to /dev/null especially for that purpose) but errors will show if anything goes wrong.




          Why did it ignore the "sudo" before "mount" ?




          As for why you're getting error message that's because the form



          reply=sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS


          is treated as envvariable=value command arg1 arg2, i.e. you're putting variable result with value sudo into environment of mount command. The command will know about it, but after command exits - the variable is gone too.




          Why did it not print the (non-zero) value of $reply ?




          Because reply doesn't store exit status as explained before. There's actually a variable for that provided by shell already, $? so use that as



          if [ $? -eq 0 ] 


          Rinse and repeat the suggestions for other statements in your script.



          Good luck







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 mins ago

























          answered 22 mins ago









          Sergiy Kolodyazhnyy

          66.9k9135297




          66.9k9135297











          • @muru Ah, right, I rushed on that one. Yep , pre-command assignments are only valid for that command's execution environment
            – Sergiy Kolodyazhnyy
            7 mins ago










          • @muru Edited. Thanks for the catch
            – Sergiy Kolodyazhnyy
            4 mins ago
















          • @muru Ah, right, I rushed on that one. Yep , pre-command assignments are only valid for that command's execution environment
            – Sergiy Kolodyazhnyy
            7 mins ago










          • @muru Edited. Thanks for the catch
            – Sergiy Kolodyazhnyy
            4 mins ago















          @muru Ah, right, I rushed on that one. Yep , pre-command assignments are only valid for that command's execution environment
          – Sergiy Kolodyazhnyy
          7 mins ago




          @muru Ah, right, I rushed on that one. Yep , pre-command assignments are only valid for that command's execution environment
          – Sergiy Kolodyazhnyy
          7 mins ago












          @muru Edited. Thanks for the catch
          – Sergiy Kolodyazhnyy
          4 mins ago




          @muru Edited. Thanks for the catch
          – Sergiy Kolodyazhnyy
          4 mins ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1088748%2fbash-scripting-problem%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

          Confectionery