0

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....

[B]#!/bin/sh[/B]
[I]In these lines, #!/bin/sh is used as the first line of a script to invoke the named [I]shell[/I].[/I]
[B]
# 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[/B]
[I]These are comment lines. Anything given on the rest of the line is passed [I]as a single argument [/I]to the named [I]shell[/I]. # is used in shell scripts as the comment character. The script typically ignores all text that follows on the same line.[/I]

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

[I]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.[/I]

[B]if [ "$(whoami)" != "root" ] ; then[/B]
[I]This line checks to see if you are logged in as the root. The semicolon separates multiple commands entered on a single line.[/I]
[B]
  echo "Error: You must be root to run this command." >&2[/B]
[I]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."[/I]
[B]
 exit 1[/B]
[I]The exit is self explanatory. The 1 represents a failure error code.[/I]
[B]
fi[/B]
[I]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.[/I]

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

[/B]
[I]This statement displays the string between the quotes $(hostname). This information is defined in the .profile file. [/I]

[B]# Adjust '5000' to match the top end of your user account namespace
# because some system accounts have uid's like 65535 and similar.[/B]
[I]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.[/I]
[B]
uid="$(awk -F: '{ if (big < $3 && $3 < 5000) big=$3 } END { print big + 1 }'[/B]
[I]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. ()[/I]
[B]
$pwfile)"
homedir=$hdir/$login[/B]
[B]# We are giving each user their own group, so gid=uid
gid=$uid[/B]

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

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

echo "${login}:x:${gid}:$login" >> $gfile
mkdir $homedir[/B]
[I]This line then creates a new directory for the user as defined above.[/I]
[B]
cp -R /etc/skel/.[a-zA-Z]* $homedir[/B]
[I]This line copies the each source file into the directory (retaining the same name).[/I]
[B]
chmod 755 $homedir[/B]
[I]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.[/I]

[B]find $homedir -print | xargs chown ${login}:${login}[/B]
[I]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.[/I]
[B]# Setting an initial password
passwd $login[/B]
[I]These line sets the initial value (password - created by administrator), then allows the user to change the created password at their first login.[/I]
[B]exit 0[/B]
[I]This line exits the script. The “0” represents a success (or no-error) code to return to the calling program.[/I]

Again thanks for the assistance.

1
Contributor
1
Reply
2
Views
10 Years
Discussion Span
Last Post by MDPGC
0

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....

[B]#!/bin/sh[/B]
[I]In these lines, #!/bin/sh is used as the first line of a script to invoke the named [I]shell[/I]. 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.[/I]

[B]# 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[/B]
[I]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.[/I]

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

[I]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.[/I]

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

[B]  echo "Error: You must be root to run this command." >&2[/B]
[I]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."[/I]

[B]exit 1[/B]
[I]If user is not the root, then the script will exit. The 1 represents a failure error code.[/I]

[B]fi[/B]
[I]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.[/I]

[B]echo "Add new user account to $(hostname)"[/B]
[I]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[/I]

[B]echo -n "login: "     ; read login

[/B]
[I]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 [/I]
[I](login: kiverson)[/I]

[B]# Adjust '5000' to match the top end of your user account namespace
# because some system accounts have uid's like 65535 and similar.[/B]
[I]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.[/I]

[B]uid="$(awk -F: '{ if (big < $3 && $3 < 5000) big=$3 } END { print big + 1 }' $pwfile)"[/B]
[I]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. ()[/I]


[B]homedir=$hdir/$login[/B]
[I]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". [/I]
[I]For example, "/home/kiverson"[/I]
[B]# We are giving each user their own group, so gid=uid
gid=$uid[/B]

[I]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.[/I]

[B]echo -n "full name: " ; read fullname[/B]
[I]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[/I]
[I](full name: Khalid Iverson)[/I]

[B]echo -n "shell: "     ; read shell[/B]
[I]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[/I]
[I](shell: Bash)[/I]
[B]echo "Setting up account $login for $fullname..."[/B][I]When full name and shell output is added the prompt confirms that an account has been set up. The script will read[/I]
[I](Setting up account kiverson for Khalid Iverson)

[/I]
[B]echo ${login}:x:${uid}:${gid}:${fullname}:${homedir}:$shell >> $pwfile[/B]
[I]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.[/I]

[B]echo ${login}:*:11647:0:99999:7::: >> $shadowfile[/B]
[I]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.[/I]


[B]echo "${login}:x:${gid}:$login" >> $gfile[/B]
[I]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.[/I]
[B]mkdir $homedir[I][/I][/B]
[I]This line then creates a new directory for the user as defined above.[/I]

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

[B]chmod 755 $homedir[/B]
[I]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.[/I]

[B]find $homedir -print | xargs chown ${login}:${login}[/B]
[I]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.[/I]
[B]# Setting an initial password[/B]
[I]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.[/I]
[B] [/B]

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

Again thanks for the assistance.

Ive reworded it a little. Again please let me know if Im on the right track

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.