The first script you need to write is login.sh, a simple script that you might run when you first log in to a machine. We'll expand this script later, but for now it should include your name, username, hostname, current directory, and the current time. Here is some sample output to help guide you. Note that the bolded lines that start with "[user@localhost ~]$" are the terminal prompts where you type in a command, not part of the script. Of course, your values will be different since you are a different user!
[user@localhost ~]$ login.sh
Welcome to localhost.localdomain, John Doe!
You are logged in as jdoe and your current directory is /home/jDoe/lab2.
The time is 11:47am. [user@localhost ~]$
There are a number of environment variables you will need to use that contain most of the information needed. Specifically, you should use the following variables: $HOSTNAME for the computer name (localhost.localdomain), $USERNAME for the log in username (sjung), and $PWD for the current directory (/home/sjung). To get the current time, use the date command. By default date prints out more than we want, but you can control the output using some funny looking syntax. Look at the help or man page for details, but you should run it like this to get the output we want:
1. The command is: date "+%l:%M%P"
2. Note that the %l is a percent sign followed by a lowercase l (ell), not a one, a pipe, or an upper or lowercase i (eye).
To get the full name of the user, you'll need to do some work. There is no environment variable that defines the full name for you, but it is available in the /etc/passwd file. You will need to use a combination of the $USERNAME environment variable and grep to get the one line from /etc/passwd for your user account, then use the cut command to get the full name from that line.
Shell Scripting As with any other program, test it to make sure it works. When it is working, TAKE A SCREENSHOT (1) of the output of the script. TAKE A SCREENSHOT (2– login.sh) your script. I can’t grade if your screenshot is not readable. Bigger font please! If you can’t control font size, please submit the file as text format.
Please find the requested script below. Also including the screenshot of sample output and screenshot of code
Please provide your feedback
Thanks and Happy learning!
output:
program:
login.sh
#!/bin/bash
fullUserName=`cat /etc/passwd | grep $USERNAME | cut -d: -f5`
echo "Welcome to $HOSTNAME, $fullUserName!"
echo "You are logged in as $USERNAME and your current directory is $PWD."
echo "The time is `date +%l:%M%P`."
Get Answers For Free
Most questions answered within 1 hours.