Answer to Question #174499 in C for Kwame Hay

Question #174499

Question 4

A) Write a shell script that does the following

Allows the user to enter seven urls into a text file.

Check to see the status of the links as in whether they are working or not

Write the name of the url and it’s status into another file.

Append to the file how many urls are working and how many are broken.

[10 marks]

B) With a working example show how IO Redirection works in shell scripting.

[7marks]

C) With examples show how each of these external commands work

 Awk

 Sed

 Grep

[4 marks]

D) Why would this process give an error

1) first create the file

touch my_example.sh

2) open it in an editor

gedit my_example.sh

3) write some code

#!/bin/sh

echo “Shell scripting is fun.”

4) run the file

./my_example.sh


1
Expert's answer
2021-04-04T17:28:20-0400

A)

#!/bin/bash

read -p "Enter 7 space separated urls: " -ra array1 # Read array elements from terminal
printf "%s\n" "${array1[@]}" > urls_file.txt # Write URLs to text file "urls_file.txt"
number_of_working_sites=0
number_of_broken_sites=0
for site in "${array1[@]}";
do
if curl -I "$site" 2>&1 | grep -w "200\|301" ; then # Check if url is working by checking the status codes(200 or 301)
echo "$site   Working" >> url_status.txt  # If url is working then write url and status
number_of_working_sites=$((++number_of_working_sites))
else
echo "$site   Broken" >> url_status.txt
number_of_broken_sites=$((++number_of_broken_sites))
fi
done
echo "Number of URLs working: $number_of_working_sites" >> url_status.txt
echo "Number of URLs broken: $number_of_broken_sites" >> url_status.txt

B) IO Redirection in shell scripting

i) Output Redirection:

cat > file.txt
Linux is awesome

file.txt(Will have the text mentioned below)

Linux is awesome

ii)Input Redirection:

cat < file.txt

Output(In Terminal):

Linux is awesome

C) Let's create an example file first to explain the working of AwkSed and Grep:

cat > urls_file.txt
https://fast.com
https://slides.com
https://dictation.io
https://www.labnol.org/reverse/
https://www.codeacademy.com/
https://www.noisli.com/
https://virusscan.jotti.org/en
http://www.abcder.com

i) awk

# Prints every line of data from the specified file "urls_file.txt"

awk {print} urls_file.txt

Output:

https://fast.com
https://slides.com
https://dictation.io
https://www.labnol.org/reverse/
https://www.codeacademy.com/
https://www.noisli.com/
https://virusscan.jotti.org/en
http://www.abcder.com

ii) sed

# Replace all occurrences of a pattern("com") with "org" in the file "urls_file.txt"

sed 's/com/org/g' urls_file.txt

Output:

https://fast.org
https://slides.org
https://dictation.io
https://www.labnol.org/reverse/
https://www.codeacademy.org/
https://www.noisli.org/
https://virusscan.jotti.org/en
http://www.abcder.org

iii) grep

# Returns the lines containing the matched string "com". "-i" indicates case insensitive search.

grep -i "cOm" urls_file.txt

Output:

https://fast.com
https://slides.com
https://www.codeacademy.com/
https://www.noisli.com/
http://www.abcder.com

D) The process will give a "permission denied" error because we are trying to access the shell script using relative path through root. We will need to change the file permission using "chmod" to execute it.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS