Interpreting a script line by line

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2007
Posts: 2
Reputation: MDPGC is an unknown quantity at this point 
Solved Threads: 0
MDPGC MDPGC is offline Offline
Newbie Poster

Interpreting a script line by line

 
0
  #1
Feb 22nd, 2007
I have a script that I have to interpret. I'm really new to shell scripting, however I have tried to explain the lines to the best of my ability. Can you please look at the script and let me know if I am on the right track (also point out if I'm off track).

Thanks in advance....

#!/bin/sh
In these lines, #!/bin/sh is used as the first line of a script to invoke the named shell.

# adduser - Adds a new user to the system, including building their
#           home directory, copying in default config data, etc.
#           For a standard Unix/Linux system
These are comment lines. Anything given on the rest of the line is passed as a single argument to the named shell. # is used in shell scripts as the comment character. The script typically ignores all text that follows on the same line.

pwfile="/etc/passwd"    shadowfile="/etc/shadow"
gfile="/etc/group"
hdir="/home"

These are assignment statements that assign values for the directory locations. On a Unix system, an account is created by adding a unique entry to the /etc/passwd file, an entry consisting of a one-to eight-character account name, a unique user ID, a group ID, a home directory (/home), and a login shell for that user. Modern Unix systems store the encrypted password value in /etc/shadow, so an entry must be added to that file too, and finally the account needs to be listed in the /etc/group file, with the user either as his or her own group or as part of an existing group.

if [ "$(whoami)" != "root" ] ; then
This line checks to see if you are logged in as the root. The semicolon separates multiple commands entered on a single line.

  echo "Error: You must be root to run this command." >&2
If you are not signed in as the root, then the script will exit with the error message, “Error: You must be root to run this command."

 exit 1
The exit is self explanatory. The 1 represents a failure error code.

fi
fi is an end statement. If the person is not a root account holder then the script will stop. It will not go any further than step above.

echo "Add new user account to $(hostname)"
echo -n "login: "     ; read login


This statement displays the string between the quotes $(hostname). This information is defined in the .profile file. 

# Adjust '5000' to match the top end of your user account namespace
# because some system accounts have uid's like 65535 and similar.
These are comment lines. # is used in shell scripts as the comment character. The script typically ignores all text that follows on the same line.

uid="$(awk -F: '{ if (big < $3 && $3 < 5000) big=$3 } END { print big + 1 }'
This line scans through the /etc/passwd file, ascertaining the largest user ID currently in use that's less than the highest allowable user account value (adjust this for your configuration preferences) and then adding 1 to it for the new account user ID. This saves the admin from having to remember what the next available ID is, and it also offers a high degree of consistency in account information as the user community evolves and changes. ()

$pwfile)"
homedir=$hdir/$login
# We are giving each user their own group, so gid=uid
gid=$uid

This section defines the group id to match the user id. This means that the users would belong to a group and that they will share security settings.

echo -n "full name: " ; read fullname
This line prompts for full name, but it does not output the trailing newline. The echo command writes character strings to standard output. 
echo -n "shell: "     ; read shell
This line prompts for shell input, but it does not output the trailing newline. The echo command writes character strings to standard output. 
echo "Setting up account $login for $fullname..."
When full name and shell output is added the prompt confirms that an account has been set up.


echo ${login}:x:${uid}:${gid}:${fullname}:${homedir}:$shell >> $pwfile
echo ${login}:*:11647:0:99999:7::: >> $shadowfile

echo "${login}:x:${gid}:$login" >> $gfile
mkdir $homedir
This line then creates a new directory for the user as defined above.

cp -R /etc/skel/.[a-zA-Z]* $homedir
This line copies the each source file into the directory (retaining the same name).

chmod 755 $homedir
This line states the permissions of the users on the directory. 755 in this case would mean that: 7 - Owner can read, write and execute 5 - The group can read and execute, but not write, 5 - Everyone else can read and execute, but not write.

find $homedir -print | xargs chown ${login}:${login}
In this line, 'find' looks in a directory, and locates every file and directory within it and every subdirectory. It then has the option of doing something with each thing that it finds. In this case, it simply prints the name of whatever it finds '-print'. Each name is piped into xargs, which runs a command on each name it receives.
# Setting an initial password
passwd $login
These line sets the initial value (password - created by administrator), then allows the user to change the created password at their first login.
exit 0
This line exits the script. The “0” represents a success (or no-error) code to return to the calling program.

Again thanks for the assistance.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 2
Reputation: MDPGC is an unknown quantity at this point 
Solved Threads: 0
MDPGC MDPGC is offline Offline
Newbie Poster

Re: Interpreting a script line by line

 
0
  #2
Feb 22nd, 2007
Originally Posted by MDPGC View Post
I have a script that I have to interpret. I'm really new to shell scripting, however I have tried to explain the lines to the best of my ability. Can you please look at the script and let me know if I am on the right track (also point out if I'm off track).

Thanks in advance....

#!/bin/sh
In these lines, #!/bin/sh is used as the first line of a script to invoke the named shell. On the first line of the script, the "#!", is the name of a program which should be used to interpret the contents of the file. In this case, the first line contains "#! /bin/sh", then the contents of the file are executed as a shell script. # is used in shell scripts as the comment character. The script typically ignores all text that follows on the same line.

# adduser - Adds a new user to the system, including building their
#           home directory, copying in default config data, etc.
#           For a standard Unix/Linux system
These are comment lines. # is used in shell scripts as the comment character. The script typically ignores all text that follows on the same line.

pwfile="/etc/passwd"    shadowfile="/etc/shadow"
gfile="/etc/group"
hdir="/home"

These are assignment statements that assign values for the directory locations. On a Unix system, an account is created by adding a unique entry to the /etc/passwd file, an entry consisting of a one-to eight-character account name, a unique user ID, a group ID, a home directory (/home), and a login shell for that user. The system stores the encrypted password value in /etc/shadow, an entry must be added to that file too, and finally the account needs to be listed in the /etc/group file, with the user either as his or her own group or as part of an existing group.

if [ "$(whoami)" != "root" ] ; then
This line checks to see if you are logged in as the root. The semicolon separates multiple commands entered on a single line.

  echo "Error: You must be root to run this command." >&2
If you are not signed in as the root, then the script will stop with the error message, “Error: You must be root to run this command."

exit 1
If user is not the root, then the script will exit. The 1 represents a failure error code.

fi
fi is an end statement. If the person is not a root account holder then the script will stop. It will not go any further than step above.

echo "Add new user account to $(hostname)"
This statement displays the string between the quotes $(hostname). $(hostname) runs the 'hostname' command, which identifies the computer or server. This information is defined in the .profile file in /etc/profile or ~/.bash_profile

echo -n "login: "     ; read login


This line prompts for login. Usually, the echo command prints something, and moves to the next line. With '-n', it stays on the same line. So the output of this line will read 
(login: kiverson)

# Adjust '5000' to match the top end of your user account namespace
# because some system accounts have uid's like 65535 and similar.
These are comment lines. # is used in shell scripts as the comment character. The script typically ignores all text that follows on the same line.

uid="$(awk -F: '{ if (big < $3 && $3 < 5000) big=$3 } END { print big + 1 }' $pwfile)"
This line scans through the /etc/passwd file, ascertaining the largest user ID currently in use that's less than the highest allowable user account value and then adding 1 to it for the new account user ID. This saves the admin from having to remember what the next available ID is, and it also offers a high degree of consistency in account information as the user community evolves and changes. ()


homedir=$hdir/$login
This line states that the $hdir is the base for home directories (for instance, /home), $login is the username you're creating (kiverson). So homedir is going to be "/home/$login". 
For example, "/home/kiverson"
# We are giving each user their own group, so gid=uid
gid=$uid

This section is a comment line and defines that the group id to match the user id. This means that the users would belong to a group and that they will share security settings.

echo -n "full name: " ; read fullname
This line prompts for full name. Usually, the echo command prints something, and moves to the next line. With '-n', it stays on the same line. So the output of this line will read
(full name: Khalid Iverson)

echo -n "shell: "     ; read shell
This line prompts for shell input, Usually, the echo command prints something, and moves to the next line. With '-n', it stays on the same line. So the output of this line will read
(shell: Bash)
echo "Setting up account $login for $fullname..."When full name and shell output is added the prompt confirms that an account has been set up. The script will read
(Setting up account kiverson for Khalid Iverson)


echo ${login}:x:${uid}:${gid}:${fullname}:${homedir}:$shell >> $pwfile
This line states the user (ex. kiverson:x:11647:0:99999:/home:bash) information printed on the command line and will be written or copied to the users $pwfile in /etc/passwd.

echo ${login}:*:11647:0:99999:7::: >> $shadowfile
This line states the user (ex. kiverson:x:11647:0:99999:7:::) information will be printed on the command line and written or copied to the users $shadowfile in /etc/shadow.


echo "${login}:x:${gid}:$login" >> $gfile
This line states the user (ex. kiverson:x99999:kiverson) information will be printed on the command line and written or copied to the users $gfile in /etc/group.
mkdir $homedir
This line then creates a new directory for the user as defined above.

cp -R /etc/skel/.[a-zA-Z]* $homedir
This line copies the each source file into the directory (retaining the same name).

chmod 755 $homedir
This line states the permissions of the users on the directory. 755 in this case would mean that: 7 - Owner can read, write and execute 5 - The group can read and execute, but not write, 5 - Everyone else can read and execute, but not write.

find $homedir -print | xargs chown ${login}:${login}
In this line, 'find' looks in a directory, and locates every file and directory within it and every subdirectory. It then has the option of doing something with each thing that it finds. In this case, it simply prints the name of whatever it finds '-print'. Each name is piped into xargs, which runs a command on each name it receives.
# Setting an initial password
This is a comment lines. # is used in shell scripts as the comment character. The script typically ignores all text that follows on the same line.


passwd $login
These line sets the initial value (password - created by administrator), then allows the user to change the created password at their first login.
exit 0
This line exits the script. The “0” represents a success (or no-error) code to return to the calling program.

Again thanks for the assistance.
Ive reworded it a little. Again please let me know if Im on the right track
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC