Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Linux scripting, write a series of scripts that work with regular expressions to

ID: 672169 • Letter: L

Question

Linux scripting, write a series of scripts that work with regular expressions together with other tools and utilities to get a feel for different ways that regexs can be useful. Please make sure the code works thanks.

The first script is validate.sh. This is a simple form validation script that will be used to verify the inputs given. Normally, this would be done to validate input from a website or another program before entry into a database or other record storage. In this case, we will keep it simple and only focus on the input validation step. In particular, the script should prompt the user for four values: first name, last name, zip code, and email address. Each input must be validated, meaning that the input conforms to a certain standard. Here is the standard for this assignment: •

The first name must start with a capital letter and contain only letters and hyphens. •

The last name must start with a capital letter and contain only letters and hyphens. •

The zip code must be exactly 5 digits and nothing else. •

The email address must be a valid email address format, meaning USER@DOMAIN, where both USER and DOMAIN must be only letters, numbers, dots, underscores, and hyphens.

As the script reads each value, use regular expressions to ensure that the value matches the standard above. If a value does not match the standard, print an error message and exit immediately (don't prompt for the rest of the inputs). If all four values are validated successfully, then print a message indicating that fact. Here are some sample runs:

   [user@localhost joe]$ ./validate.sh
   
   First name: R2-­D2
   First name must start with a capital letter and contain only letters and hyphens!
   [user@localhost joe]$ ./validate.sh
   
   First name: Han
   Last name: S0L0
   Last name must start with a capital letter and contain  only letters and hyphens!
   [user@localhost joe]$ ./validate.sh

  First name: Han

Last name: Solo
Zip code: 12 parsecs
Zip code must be exactly 5 digits!

[user@localhost joe]$ ./validate.sh
First name: Han
Last name: Solo
Zip code: 12345
Email address: han.solo
Email address must be USER@DOMAIN,
where both USER and DOMAIN must be only letters, numbers, dots, underscores, and hyphens!
[user@localhost joe]$ ./validate.sh
   First name: Han
   Last name: Solo
   Zip code: 12345
   Email address: han.solo@gmail.com
  Validated!
   [user@localhost joe]$
Once you've got it tested and working, TAKE A SCREENSHOT (1) of the output of the script for a few sample runs. TAKE A SCREENSHOT (2) – validate.sh) your script.

The second script for this assignment is find_and_replace.sh. You will need to use sed to replace all instances of one string in a file. Specifically, find_and_replace.sh should take 4 arguments. The first is the file to read from, the second is the file to output to, the third is the string to find, and the fourth is the replacement string. The script itself is relatively simple. The script must read every line from the input file no matter how many lines there are. It then replaces all instances of the first string given with the second string. Finally, it outputs every line to the output file whether or not any substitutions happened.

As always you need to do error checking. Specifically, check the number of arguments and that the first argument is actually a file. Note that you don't need to check if the second argument exists or because your script should output to that file either way. In addition, you'll need a way to send the input file in to a loop to read every line of that file. You can use I/O redirection with the entire loop. The same idea will be used to send the output of the loop to the output file. Here is some example output:

[user@localhost joe]$ ./find_and_replace.sh
   usage: find_and_replace.sh existing_file new_file current_string new_string
   [user@localhost joe]$ cat jedi_code
   Jedi code:
   There is no emotion, there is peace.
   There is no ignorance, there is knowledge.
   There is no passion, there is serenity.
   There is no chaos, there is harmony.
   There is no death, there is the Force.
   [user@localhost
  joe]$ ./find_and_replace.sh jedi_code
  jedi.out "is no" "ain't"
  [user@localhost joe]$ cat jedi.out
  Jedi code:
There ain't emotion, there is peace.
   
   There ain't ignorance, there is knowledge.
   
   There ain't passion, there is serenity.
   
   There ain't chaos, there is harmony.
   There ain't death, there is the Force.
   [user@localhost joe]$
  
Once you've got it tested and working, TAKE A SCREENSHOT (3) of the output of the script for a sample run. TAKE A SCREENSHOT (4) – find_and replace.sh) your script.

The third script is get_external_ip.sh, whose purpose is to find the public/external IP address that you are currently using to connect to the Internet. The IP address assigned to your laptop is almost certainly a private address, i.e., one that can't be used to communicate with other computers outside of your LAN. When your packets leave the LAN, a Network Address Translation device rewrites parts of your packets so that they get one of the public IP addresses that are available for your LAN. There are many websites that will tell your current public IP address, so we will use one of them to do this. Let's see that first:

1. Open up your web browser and go to: www.ipchicken.com

2. The web page will tell you your current public IP address.

Of course, web pages are just plain text HTML documents, and so we can write a script that downloads the web page and pulls out the IP address using a regex. There are many ways to think about the regular expression, but by far the simplest is to only match strings that look like IP addresses. To get started, use wget to download the webpage to a local file (wget www.ipchicken.com). Look through the file to find the IP address inside. Then use grep to search through the file for strings that look like IP addresses and only print those out. Of course, grep will print out the entire line that matches, but there is an option to grep that will have it only print out the actual matching string so you'll want to find that. Then, remove the downloaded file to clean up after yourself. Once you get the steps correct by hand, you can put them into the script which should be three lines long. Here is a sample run:
  [username@localhost joe]$ ./get_external_ip.sh
173.48.162.122
  [username@localhost joe]$


Once you've got it tested and working, TAKE A SCREENSHOT (5) of the output of the script for a sample run. TAKE A SCREENSHOT (6) – get_external_ip.sh) your script.

  
The final script is account_list.sh that is going to print a summary of some account information on your system. We've been looking at the /etc/passwd file on and off. This script is going to parse that file to find and print the usernames for all non-system accounts. In addition, if the account has a non-standard (i.e., non-bash) shell, then the script will mark that username with an asterisk.

In particular, when you look at a line of /etc/passwd, there are seven colon-delimited fields. The first is the username, the third is the user ID, and the seventh is the shell for that user. Your script must ignore all system accounts, which are accounts that have a user ID of less than 1000. If the account has an ID of at least 1000, then you will print the username of that account. If the shell for that account is not /bin/bash, then also print an asterisk after the name to indicate that it is non-standard. Of course, you'll need to build in to the script to read from /etc/passwd with a while loop. Then you should be using awk to extract the appropriate fields from each line. Here is a hint:


Here is a sample run:
  [username@localhost joe]$ ./account_list.sh
  nfsnobody*
  username
  [username@localhost joe]$

while read line;
  do
   # use  awk here
done < /etc/passwd

Explanation / Answer

Printing a range of lines

# 1. just lines 15 to 17 perl -ne 'print if 15 .. 17' # 2. just lines NOT between line 10 and 20 perl -ne 'print unless 10 .. 20' # 3. lines between START and END perl -ne 'print if /^START$/ .. /^END$/' # 4. lines NOT between START and END perl -ne 'print unless /^START$/ .. /^END$/'

A problem with the first one-liner in Listing 3 is that it will go through the whole file, even if the necessary range has already been covered. The third one-liner does not have that problem, because it will print all the lines between the START and END markers. If there are eight sets of START/END markers, the third one-liner will print the lines inside all eight sets.

Preventing the inefficiency of the first one-liner is easy: just use the $. variable, which tells you the current line. Start printing if $. is over 15 and exit if $. is greater than 17.

In-place editing

In-place editing

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote