Bash script using output of mount as a variable throws numerous errors

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











up vote
3
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
    3
    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
      3
      down vote

      favorite









      up vote
      3
      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?






      command-line bash scripts






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 12 mins ago









      Zanna

      48.6k13120230




      48.6k13120230










      asked 9 hours ago









      Dave Kimble

      192




      192




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          7
          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 command's environment, but the shell running the script knows nothing of it - it doesn't exist. To 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 is 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






















          • Thanks, working now. What a horrible syntax
            – Dave Kimble
            8 hours ago






          • 1




            @DaveKimble Yeah, it takes time getting used to the syntax in shell scripting. But eventually you'll get it with enough practice and reading answers here and on unix.stackexchange.com or stackoverflow. And you can always try Python. Syntax is nicer there and faster but you have to call external commands like mount via Popen module. Btw, if the answer solved the problem, don't forget to mark it as accepted ( grey checkmark ). Or leave it unmarked. It's not a requirement.
            – Sergiy Kolodyazhnyy
            8 hours ago

















          up vote
          0
          down vote













          Problems with your script:



          • You missed ` for sudo mount .. command. To execute a command, you need to put it back quotes `command` or $(command). You can capture return value of the script using $? as well.


          • If loop construct was wrong



            if [ $var -eq/-lt/-gt "string"/number ]
            then
            echo "...."
            else
            echo "..."
            fi; <=== Most users miss this.


          ** Edited script**



          #!/bin/bash

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

          if [ $reply -eq "0" ]
          then
          echo "NFS mounted OK"
          else
          echo "Mounting NFS failed: $reply"
          fi;

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





          share|improve this answer






















          • Most users miss what? The semicolon after fi is definitely not needed. And command substitution has been mentioned by Sergiy already. Note that $(command) should be preferred to backticks.
            – Melebius
            5 hours ago


















          up vote
          0
          down vote













          In addition to Sergiy's good answer, you could consider using AND and OR lists to quickly check if a command returns an exit status of zero (succeeded) or not, for example



          command && echo "command succeeded" || echo "command failed"


          Here's an excerpt from man bash:




          AND and OR lists are sequences of one or more pipelines separated by
          the && and || control operators, respectively. AND and OR lists are
          executed with left associativity. An AND list has the form



           command1 && command2


          command2 is executed if, and only if, command1 returns an exit status
          of zero.



          An OR list has the form



           command1 || command2


          command2 is executed if and only if command1 returns a non-zero exit
          status. The return status of AND and OR lists is the exit status of
          the last command executed in the list.




          You could combine them, and also see the exit status if you want, with your example like:



          sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS && 
          echo "NFS mounted OK" || echo "Mounting NFS failed: $?"





          share|improve this answer




















            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-script-using-output-of-mount-as-a-variable-throws-numerous-errors%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            7
            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 command's environment, but the shell running the script knows nothing of it - it doesn't exist. To 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 is 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






















            • Thanks, working now. What a horrible syntax
              – Dave Kimble
              8 hours ago






            • 1




              @DaveKimble Yeah, it takes time getting used to the syntax in shell scripting. But eventually you'll get it with enough practice and reading answers here and on unix.stackexchange.com or stackoverflow. And you can always try Python. Syntax is nicer there and faster but you have to call external commands like mount via Popen module. Btw, if the answer solved the problem, don't forget to mark it as accepted ( grey checkmark ). Or leave it unmarked. It's not a requirement.
              – Sergiy Kolodyazhnyy
              8 hours ago














            up vote
            7
            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 command's environment, but the shell running the script knows nothing of it - it doesn't exist. To 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 is 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






















            • Thanks, working now. What a horrible syntax
              – Dave Kimble
              8 hours ago






            • 1




              @DaveKimble Yeah, it takes time getting used to the syntax in shell scripting. But eventually you'll get it with enough practice and reading answers here and on unix.stackexchange.com or stackoverflow. And you can always try Python. Syntax is nicer there and faster but you have to call external commands like mount via Popen module. Btw, if the answer solved the problem, don't forget to mark it as accepted ( grey checkmark ). Or leave it unmarked. It's not a requirement.
              – Sergiy Kolodyazhnyy
              8 hours ago












            up vote
            7
            down vote










            up vote
            7
            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 command's environment, but the shell running the script knows nothing of it - it doesn't exist. To 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 is 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 command's environment, but the shell running the script knows nothing of it - it doesn't exist. To 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 is 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 9 hours ago









            muru

            132k19280478




            132k19280478










            answered 9 hours ago









            Sergiy Kolodyazhnyy

            67k9135297




            67k9135297











            • Thanks, working now. What a horrible syntax
              – Dave Kimble
              8 hours ago






            • 1




              @DaveKimble Yeah, it takes time getting used to the syntax in shell scripting. But eventually you'll get it with enough practice and reading answers here and on unix.stackexchange.com or stackoverflow. And you can always try Python. Syntax is nicer there and faster but you have to call external commands like mount via Popen module. Btw, if the answer solved the problem, don't forget to mark it as accepted ( grey checkmark ). Or leave it unmarked. It's not a requirement.
              – Sergiy Kolodyazhnyy
              8 hours ago
















            • Thanks, working now. What a horrible syntax
              – Dave Kimble
              8 hours ago






            • 1




              @DaveKimble Yeah, it takes time getting used to the syntax in shell scripting. But eventually you'll get it with enough practice and reading answers here and on unix.stackexchange.com or stackoverflow. And you can always try Python. Syntax is nicer there and faster but you have to call external commands like mount via Popen module. Btw, if the answer solved the problem, don't forget to mark it as accepted ( grey checkmark ). Or leave it unmarked. It's not a requirement.
              – Sergiy Kolodyazhnyy
              8 hours ago















            Thanks, working now. What a horrible syntax
            – Dave Kimble
            8 hours ago




            Thanks, working now. What a horrible syntax
            – Dave Kimble
            8 hours ago




            1




            1




            @DaveKimble Yeah, it takes time getting used to the syntax in shell scripting. But eventually you'll get it with enough practice and reading answers here and on unix.stackexchange.com or stackoverflow. And you can always try Python. Syntax is nicer there and faster but you have to call external commands like mount via Popen module. Btw, if the answer solved the problem, don't forget to mark it as accepted ( grey checkmark ). Or leave it unmarked. It's not a requirement.
            – Sergiy Kolodyazhnyy
            8 hours ago




            @DaveKimble Yeah, it takes time getting used to the syntax in shell scripting. But eventually you'll get it with enough practice and reading answers here and on unix.stackexchange.com or stackoverflow. And you can always try Python. Syntax is nicer there and faster but you have to call external commands like mount via Popen module. Btw, if the answer solved the problem, don't forget to mark it as accepted ( grey checkmark ). Or leave it unmarked. It's not a requirement.
            – Sergiy Kolodyazhnyy
            8 hours ago












            up vote
            0
            down vote













            Problems with your script:



            • You missed ` for sudo mount .. command. To execute a command, you need to put it back quotes `command` or $(command). You can capture return value of the script using $? as well.


            • If loop construct was wrong



              if [ $var -eq/-lt/-gt "string"/number ]
              then
              echo "...."
              else
              echo "..."
              fi; <=== Most users miss this.


            ** Edited script**



            #!/bin/bash

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

            if [ $reply -eq "0" ]
            then
            echo "NFS mounted OK"
            else
            echo "Mounting NFS failed: $reply"
            fi;

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





            share|improve this answer






















            • Most users miss what? The semicolon after fi is definitely not needed. And command substitution has been mentioned by Sergiy already. Note that $(command) should be preferred to backticks.
              – Melebius
              5 hours ago















            up vote
            0
            down vote













            Problems with your script:



            • You missed ` for sudo mount .. command. To execute a command, you need to put it back quotes `command` or $(command). You can capture return value of the script using $? as well.


            • If loop construct was wrong



              if [ $var -eq/-lt/-gt "string"/number ]
              then
              echo "...."
              else
              echo "..."
              fi; <=== Most users miss this.


            ** Edited script**



            #!/bin/bash

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

            if [ $reply -eq "0" ]
            then
            echo "NFS mounted OK"
            else
            echo "Mounting NFS failed: $reply"
            fi;

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





            share|improve this answer






















            • Most users miss what? The semicolon after fi is definitely not needed. And command substitution has been mentioned by Sergiy already. Note that $(command) should be preferred to backticks.
              – Melebius
              5 hours ago













            up vote
            0
            down vote










            up vote
            0
            down vote









            Problems with your script:



            • You missed ` for sudo mount .. command. To execute a command, you need to put it back quotes `command` or $(command). You can capture return value of the script using $? as well.


            • If loop construct was wrong



              if [ $var -eq/-lt/-gt "string"/number ]
              then
              echo "...."
              else
              echo "..."
              fi; <=== Most users miss this.


            ** Edited script**



            #!/bin/bash

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

            if [ $reply -eq "0" ]
            then
            echo "NFS mounted OK"
            else
            echo "Mounting NFS failed: $reply"
            fi;

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





            share|improve this answer














            Problems with your script:



            • You missed ` for sudo mount .. command. To execute a command, you need to put it back quotes `command` or $(command). You can capture return value of the script using $? as well.


            • If loop construct was wrong



              if [ $var -eq/-lt/-gt "string"/number ]
              then
              echo "...."
              else
              echo "..."
              fi; <=== Most users miss this.


            ** Edited script**



            #!/bin/bash

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

            if [ $reply -eq "0" ]
            then
            echo "NFS mounted OK"
            else
            echo "Mounting NFS failed: $reply"
            fi;

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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 6 hours ago









            muru

            132k19280478




            132k19280478










            answered 6 hours ago









            user3409057

            1




            1











            • Most users miss what? The semicolon after fi is definitely not needed. And command substitution has been mentioned by Sergiy already. Note that $(command) should be preferred to backticks.
              – Melebius
              5 hours ago

















            • Most users miss what? The semicolon after fi is definitely not needed. And command substitution has been mentioned by Sergiy already. Note that $(command) should be preferred to backticks.
              – Melebius
              5 hours ago
















            Most users miss what? The semicolon after fi is definitely not needed. And command substitution has been mentioned by Sergiy already. Note that $(command) should be preferred to backticks.
            – Melebius
            5 hours ago





            Most users miss what? The semicolon after fi is definitely not needed. And command substitution has been mentioned by Sergiy already. Note that $(command) should be preferred to backticks.
            – Melebius
            5 hours ago











            up vote
            0
            down vote













            In addition to Sergiy's good answer, you could consider using AND and OR lists to quickly check if a command returns an exit status of zero (succeeded) or not, for example



            command && echo "command succeeded" || echo "command failed"


            Here's an excerpt from man bash:




            AND and OR lists are sequences of one or more pipelines separated by
            the && and || control operators, respectively. AND and OR lists are
            executed with left associativity. An AND list has the form



             command1 && command2


            command2 is executed if, and only if, command1 returns an exit status
            of zero.



            An OR list has the form



             command1 || command2


            command2 is executed if and only if command1 returns a non-zero exit
            status. The return status of AND and OR lists is the exit status of
            the last command executed in the list.




            You could combine them, and also see the exit status if you want, with your example like:



            sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS && 
            echo "NFS mounted OK" || echo "Mounting NFS failed: $?"





            share|improve this answer
























              up vote
              0
              down vote













              In addition to Sergiy's good answer, you could consider using AND and OR lists to quickly check if a command returns an exit status of zero (succeeded) or not, for example



              command && echo "command succeeded" || echo "command failed"


              Here's an excerpt from man bash:




              AND and OR lists are sequences of one or more pipelines separated by
              the && and || control operators, respectively. AND and OR lists are
              executed with left associativity. An AND list has the form



               command1 && command2


              command2 is executed if, and only if, command1 returns an exit status
              of zero.



              An OR list has the form



               command1 || command2


              command2 is executed if and only if command1 returns a non-zero exit
              status. The return status of AND and OR lists is the exit status of
              the last command executed in the list.




              You could combine them, and also see the exit status if you want, with your example like:



              sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS && 
              echo "NFS mounted OK" || echo "Mounting NFS failed: $?"





              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                In addition to Sergiy's good answer, you could consider using AND and OR lists to quickly check if a command returns an exit status of zero (succeeded) or not, for example



                command && echo "command succeeded" || echo "command failed"


                Here's an excerpt from man bash:




                AND and OR lists are sequences of one or more pipelines separated by
                the && and || control operators, respectively. AND and OR lists are
                executed with left associativity. An AND list has the form



                 command1 && command2


                command2 is executed if, and only if, command1 returns an exit status
                of zero.



                An OR list has the form



                 command1 || command2


                command2 is executed if and only if command1 returns a non-zero exit
                status. The return status of AND and OR lists is the exit status of
                the last command executed in the list.




                You could combine them, and also see the exit status if you want, with your example like:



                sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS && 
                echo "NFS mounted OK" || echo "Mounting NFS failed: $?"





                share|improve this answer












                In addition to Sergiy's good answer, you could consider using AND and OR lists to quickly check if a command returns an exit status of zero (succeeded) or not, for example



                command && echo "command succeeded" || echo "command failed"


                Here's an excerpt from man bash:




                AND and OR lists are sequences of one or more pipelines separated by
                the && and || control operators, respectively. AND and OR lists are
                executed with left associativity. An AND list has the form



                 command1 && command2


                command2 is executed if, and only if, command1 returns an exit status
                of zero.



                An OR list has the form



                 command1 || command2


                command2 is executed if and only if command1 returns a non-zero exit
                status. The return status of AND and OR lists is the exit status of
                the last command executed in the list.




                You could combine them, and also see the exit status if you want, with your example like:



                sudo mount 192.168.0.2:/home/dk/NFS /home/dk/NFS && 
                echo "NFS mounted OK" || echo "Mounting NFS failed: $?"






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 18 mins ago









                Xen2050

                6,46912142




                6,46912142



























                     

                    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-script-using-output-of-mount-as-a-variable-throws-numerous-errors%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    Confectionery