Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Explain bash script?

  1. #1
    Join Date
    Jan 2013
    Beans
    28
    Distro
    Edubuntu

    Explain bash script?

    There is one bash script. I want to make other scrpt similar to the first one, but do not quite understand some things in this script. Can someone help me with this?


    This part of the script in which I want to understand.
    Code:
    pfile=/etc/passwd
    
    echo
    LOGIN="$1"
    needinput=yes
    while [ ! -z $needinput ]; do
      if [ -z "$LOGIN" ]; then 
        while [ -z "$LOGIN" ]; do LOGIN="$(get_input "Login name for new user []:")" ; done
      fi
      grep "^${LOGIN}:" $pfile >/dev/null 2>&1  # ensure it's not already used
      if [ $? -eq 0 ]; then
        echo "- User '$LOGIN' already exists; please choose another"
        unset LOGIN
      elif [ ! -z "$( echo $LOGIN | grep "^[0-9]" )" ]; then
        echo "- User names cannot begin with a number; please choose another"
        unset LOGIN
      elif [ ! "$LOGIN" = "`echo $LOGIN | tr A-Z a-z`" ]; then # useradd does not allow uppercase
        echo "- User '$LOGIN' contains illegal characters (uppercase); please choose another"
        unset LOGIN
      elif [ ! -z "$( echo $LOGIN | grep '\.' )" ]; then
        echo "- User '$LOGIN' contains illegal characters (period/dot); please choose another"
        unset LOGIN
      else
        unset needinput
      fi
    done
    1. Why firstly variable LOGIN is set to $ 1?
    This is in case the script is run with an additional variable in the command line? Like:

    Code:
    somescript.sh username
    2. What does mean options "-z" in

    Code:
    while [ ! -z $needinput ]; do
      if [ -z "$LOGIN" ]; then 
        while [ -z "$LOGIN" ]; do LOGIN="$(get_input "Login name for new user []:")" ; done
    3. I do not quite understand how the script looks for a new login to the already existing list of users. What does it mean?

    Code:
    2>&1

    PS: Thnx for your help for noob like me.
    Last edited by CrewDK; February 6th, 2013 at 10:48 AM.

  2. #2
    iMac71 is offline Gee! These Aren't Roasted!
    Join Date
    Dec 2012
    Beans
    171

    Re: Explain bash script?

    a) $1 is the first argoment passed to the script: see here:
    http://tldp.org/LDP/abs/html/othertypesv.html

    b) -z is a comparison operator for verifying if a string is null: see here:
    http://tldp.org/LDP/abs/html/comparison-ops.html

    c) 2>&1 means that the output is sent to stderr (file descriptor #2) in addition to stdout (file descriptor #1): see here:
    http://tldp.org/LDP/abs/html/io-redirection.html
    Last edited by iMac71; February 6th, 2013 at 11:14 AM.

  3. #3
    squakie is offline I Ubuntu, Therefore, I Am
    Join Date
    Oct 2012
    Beans
    2,238
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Explain bash script?

    Perhaps if you could tell us what you are changing it to and why we might be able to help more.

  4. #4
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Explain bash script?

    Not that it were important or changed something, but I'd rewrite the code like this
    Code:
    echo
    LOGIN="$1"
    another='please choose another'
    
    while
      while [[ -z $LOGIN ]]
        do LOGIN="$(get_input "Login name for new user []:")"
      done
    do
      if getent passwd "$LOGIN" &>/dev/null; then
        echo "- User '$LOGIN' already exists;" $another
      else
        case $LOGIN in
        [0-9]*)
          echo "- User names cannot begin with a number;" $another
          ;;
        *[A-Z]*)
          echo "- User '$LOGIN' contains illegal characters (uppercase);" $another
          ;;
        *.*)
          echo "- User '$LOGIN' contains illegal characters (period/dot);" $another
          ;;
        *) break
        esac
      fi
      unset LOGIN
    done

  5. #5
    Join Date
    Jan 2013
    Beans
    28
    Distro
    Edubuntu

    Re: Explain bash script?

    Quote Originally Posted by iMac71 View Post
    a) $1 is the first argoment passed to the script: see here:
    http://tldp.org/LDP/abs/html/othertypesv.html

    b) -z is a comparison operator for verifying if a string is null: see here:
    http://tldp.org/LDP/abs/html/comparison-ops.html

    c) 2>&1 means that the output is sent to stderr (file descriptor #2) in addition to stdout (file descriptor #1): see here:
    http://tldp.org/LDP/abs/html/io-redirection.html
    Thanks. I'll read all this man's.

    Quote Originally Posted by squakie
    Perhaps if you could tell us what you are changing it to and why we might be able to help more.
    Well, I'm working as a sort of administrator in a normal school in Russia.
    At the moment I'm trying to reinstall most of the computers in our school from windows to Linux.
    The problem is that there is some part of the training programs (on CD or DVD f.x.) that don't work under wine, but only under winxp so I have to install on some machines VirtualBox.

    At school, there is not a domain, so I have to setup all the computers separately.
    So I want to write a script that would:

    - ask login (username) of the target user and check whether there exist such login on the computer;
    - download, install package with VirtualBox (already done);
    - download, unpack the archive with VBox HDD (with installed WinXP) and settings to user's home directory and fix the path to user's home directory in the vbox configs. (almost done)
    Last edited by CrewDK; February 6th, 2013 at 02:01 PM.

  6. #6
    Join Date
    Jan 2013
    Beans
    28
    Distro
    Edubuntu

    Re: Explain bash script?

    Quote Originally Posted by schragge View Post
    Not that it were important or changed something, but I'd rewrite the code like this
    Code:
    echo
    LOGIN="$1"
    another='please choose another'
    
    while
      while [[ -z $LOGIN ]]
        do LOGIN="$(get_input "Login name for new user []:")"
      done
    do
      if getent passwd "$LOGIN" &>/dev/null; then
        echo "- User '$LOGIN' already exists;" $another
      else
        case $LOGIN in
        [0-9]*)
          echo "- User names cannot begin with a number;" $another
          ;;
        *[A-Z]*)
          echo "- User '$LOGIN' contains illegal characters (uppercase);" $another
          ;;
        *.*)
          echo "- User '$LOGIN' contains illegal characters (period/dot);" $another
          ;;
        *) break
        esac
      fi
      unset LOGIN
    done
    Thnx. But this part of script i need only like example for my other script. BTW, initialy this is rather big script for adding user, managing user's groups e.t.c., some sort of adduser\useradd but with advanced features.
    Last edited by CrewDK; February 6th, 2013 at 02:00 PM.

  7. #7
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Explain bash script?

    Quote Originally Posted by CrewDK View Post
    - ask login (username) of the target user and check whether there exist such login on the computer;
    If I understand correctly, that's the part you're working on now. You can check for existence of a user with getent(1) like this
    Code:
    if getent passwd "$LOGIN" >/dev/null
    then
      echo User found
    else
      echo User not found
    fi

  8. #8
    Join Date
    Jan 2013
    Beans
    28
    Distro
    Edubuntu

    Re: Explain bash script?

    Quote Originally Posted by iMac71 View Post

    c) 2>&1 means that the output is sent to stderr (file descriptor #2) in addition to stdout (file descriptor #1): see here:
    http://tldp.org/LDP/abs/html/io-redirection.html
    Do I understand right that in this case, I just do not see any messages (about error for example)?

    Quote Originally Posted by schragge View Post
    If I understand correctly, that's the part you're working on now. You can check for existence of a user with getent(1) like this
    Code:
    if getent passwd "$LOGIN" >/dev/null
    then
      echo User found
    else
      echo User not found
    fi
    Yes, you understand correctly. Thanx for this idea. But I think, that's it'll be better keep some checks for correct username in my script.
    Last edited by CrewDK; February 6th, 2013 at 02:45 PM.

  9. #9
    iMac71 is offline Gee! These Aren't Roasted!
    Join Date
    Dec 2012
    Beans
    171

    Re: Explain bash script?

    Quote Originally Posted by CrewDK View Post
    Do I understand right that in this case, I just do not see any messages (about error for example)?
    in your script
    Code:
    >/dev/null 2>&1
    means that error messages are not displayed on the screen, i.e. your script runs quietly.

  10. #10
    Join Date
    Jan 2013
    Beans
    28
    Distro
    Edubuntu

    Re: Explain bash script?

    One more question. I want to add another variable to the script, which would mean the amount of RAM for future virtual machine.
    How can I set check for the amount of allocated memory, that it's not greater than, for example, 1\2 all your memory?

    Code:
    echo
    RAM="$2"
    needinput=yes
    while [ ! -z $needinput ]; do
      if [ -z "$RAM" ]; then
        while [ -z "$RAM" ]; do RAM="$(get_input "RAM size for VBox? []:")" ; done
      fi
      grep "^${RAM}:" $pfile >/dev/null 2>&1  # <<---------- how can I check correct RAM size 
      if [ $? -ne 0 ]; then
        echo "- '$RAM' size is too big; please choose another"
        unset RAM
      else
        unset needinput
      fi
    done

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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