Results 1 to 10 of 10

Thread: sh script error

  1. #1

    Default sh script error

    Good day!
    I use openSuse 12.1.
    I had wrote this script:

    Code:
    #!/bin/bash
    
    LOG_DIR=/var/log
    ROOT_UID=0
    E_NOTROOT=67 
    array=(This is New Array)
    
        date -u +%s
        id 
        $id = id -u
        echo $id
        $variable = date -u +%s 
        echo $varialbe
    
       if [ "$UID" -ne "$ROOT_UID" ]
          then  
             echo "You must have root privileges"
             exit $E_NOTROOT
          fi
    
       if [ -n "$1" ]  
       then 
        $lines=$0
       fi    
    
        if [ lines != NULL ]
          then 
             echo $lines;
          fi   
            read input      
            echo $input
    
         for line in ${array[@]}
           do
             printf " " $item  
           done
    
    case $input in
    
      "E" | "e")
    
    echo "You select e" 
    
    if ! [-d /path/directory/ ]; then 
       f=$(mkdir -p /home/vladimir/Downloads/siteArchive);
       echo f;
       if [ f -ne 0];
       then 
         echo "Dirrectory was created"
         tar -cvzf webSite2.tar.gz /home/vladimir/Downloads/siteArchive/
         echo "Archive was created"
       else 
         echo "Dirrectory was not created " 
    else 
    echo "Dirrectory already exist";
    fi
    ;;
    
      "J" | "j")
    
    echo "You select j" 
    
    if ! [-d /path/directory/ ]; then 
       f=$(mkdir -p /home/vladimir/Downloads/siteArchive);
       echo f;
     if [ f -ne 0];
       then 
         echo "Dirrectory was created";
         tar -cvzf webSite2.tar.gz /home/vladimir/Downloads/siteArchive/;
         echo "Archive was created";
       else 
         echo "Dirrectory was not created ";  
    else 
    echo "Dirrectory already exist";
    fi
    ;;   
    esac
    and output of it is:
    "
    1393603928
    uid=0(root) gid=0(root) groups=0(root)
    ./11: line 11: =: command not found
    ./11: line 15: =: command not found
    ./11: line 70: syntax error near unexpected token `else'
    ./11: line 70: `else '
    "

    Correctly my questions is:

    1) How to define dialect of shell of my system?
    2) How correct to assign values to variables?
    3) What is the syntax of: "if - then - else - done" and "for - in - do -done" ?

  2. #2
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,728

    Default Re: sh script error

    To begin with, this is not an sh (POSIX shell) script (as you mention in your thread title), but a bash (GNU Bourne-Again Shell)) script a the shebang in your script says.

    Then, why isn't the execution of your shell (prompt, command, the output and the next prompt) posted between CODE tags, like your script itself is? Please put ALL computer ooutput facts between CODE tags here.

    Then about your questions:
    1. What "dialects" do you mean?
    2. You can assign values to v ariables in a lot of ways, but the most obvious one is
      Code:
      VARIABLE=value
    3. There is no if - then - else - done construct; it is if - then - else - fi. You can find all those things in
      Code:
      man bash
      .

    In general, my advice is to first study some docs about bash programming. There is enough on the internet (googling with "learning bash" gives you abundant info). Then try small scripts to see if you understand what you are doing and do not start with a 30-40 line script wich will be full of misunderstandings when you do not learn step by step.
    Henk van Velden

  3. #3

    Default Re: sh script error

    I want to add a few things for clarification:
    Quote Originally Posted by hcvv View Post
    You can assign values to v ariables in a lot of ways, but the most obvious one is
    Code:
    VARIABLE=value
    You have such lines in your script:
    Code:
    $id = id -u
    That's wrong in three ways! First you take the value of $id on the left side of the '=', second there should be no space before and after the '=', third you have to tell bash to execute that command ("id -u") and take the output for the assignment (I take it you want to assign the output of "id -u" to the variable $id, right?).

    So it should be:
    Code:
    id=`id -u`
    or
    Code:
    id=$(id -u)
    That's also true for the other assignments in your script of course.

    There is no if - then - else - done construct; it is if - then - else - fi. You can find all those things in
    Code:
    man bash
    .
    In general, my advice is to first study some docs about bash programming. There is enough on the internet (googling with "learning bash" gives you abundant info). Then try small scripts to see if you understand what you are doing and do not start with a 30-40 line script wich will be full of misunderstandings when you do not learn step by step.
    See here for a start regarding if-then-else:
    http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html
    "man bash" as suggested should tell you as well, or have a look at "info bash", that's even more detailed.
    Last edited by wolfi323; 28-Feb-2014 at 03:36.

  4. #4

    Default Re: sh script error

    wolfi323 wrote:
    > "man bash" as suggested should tell you as well, or have a look at "info
    > bash", that's even more detailed.


    Or even http://www.gnu.org/software/bash/manual/bash.html

  5. #5
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,728

    Default Re: sh script error

    Quote Originally Posted by wolfi323 View Post
    You have such lines in your script:
    Code:
    $id = id -u
    That's wrong in three ways! First you take the value of $id on the left side of the '=', second there should be no space before and after the '=', third you have to tell bash to execute that command ("id -u") and take the output for the assignment (I take it you want to assign the output of "id -u" to the variable $id, right?).

    So it should be:
    Code:
    id=`id -u`
    or
    Code:
    id=$(id -u)
    That is only true if you assume you know what the OP was wanting to code.
    As he did not tell what his purpose is, we (at least I, not being clairvoyant) can only guess.
    If the OP wants to call the = command with the arguments id and -u, the coding is correct. Only thing is then that there is no = command found. And that is what the message says
    Henk van Velden

  6. #6

    Default Re: sh script error

    Quote Originally Posted by hcvv View Post
    That is only true if you assume you know what the OP was wanting to code.
    As he did not tell what his purpose is, we (at least I, not being clairvoyant) can only guess.
    Of course this is only an assumption, but what else should the code want to achieve?
    And I think I made it clear that I'm just assuming the desired behaviour here.

    This is the most obvious thing though in this case, no need to be a clairvoyant to come to that conclusion...


    If the OP wants to call the = command with the arguments id and -u, the coding is correct. Only thing is then that there is no = command found. And that is what the message says
    ???
    "IF the OP wants to call the = command"? Why is there the "$id" before the '=' then?
    In this case "$id" is empty of course, because nothing has been assigned to it, so bash tries to run the '=' command.

  7. #7
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,728

    Default Re: sh script error

    Quote Originally Posted by wolfi323 View Post
    "IF the OP wants to call the = command"? Why is there the "$id" before the '=' then?
    In this case "$id" is empty of course, because nothing has been assigned to it, so bash tries to run the '=' command.
    I have seen a lot of obfuscated code where in fact only the one who wrote it could explain what it should do and why he wrote it in that way.
    Henk van Velden

  8. #8

    Default Re: sh script error

    Quote Originally Posted by hcvv View Post
    I have seen a lot of obfuscated code where in fact only the one who wrote it could explain what it should do and why he wrote it in that way.
    All right, please delete my post then...

  9. #9
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,728

    Default Re: sh script error

    Quote Originally Posted by wolfi323 View Post
    All right, please delete my post then...
    Why? This is an interesting discussion. I hope it will tell people to describe what they wanted, what they did, what they expected to happen and what happened when they post a question/problem here..
    Henk van Velden

  10. #10

    Default Re: sh script error

    Quote Originally Posted by hcvv View Post
    Why? This is an interesting discussion. I hope it will tell people to describe what they wanted, what they did, what they expected to happen and what happened when they post a question/problem here..
    TBH, _I_ don't really find this particular discussion interesting. I just wanted to give the OP a hint how to fix his script. I don't want to discuss now what he may or may not have had in mind when writing this script.

    I agree of course that people should clarify what they actually want to achieve when they ask for help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •