UNIX/LINUX QUESTION. Please, Solutions Only. Thanks! 1. Use the FOR loop to writ
ID: 3735665 • Letter: U
Question
UNIX/LINUX QUESTION. Please, Solutions Only. Thanks!
1. Use the FOR loop to write a shell script that adds an extension “.bak” to all the files in a directory by changing all the files in a directory to a “.bak” extension. Kindly make sure to supply the directory name as an argument while running the test script.
2. Use any loop technique to rewrite your shell script to validate password strength. Your program should prompt user for a password based on the following requirements:
-Length should be a minimum of 8 characters.
-Contain both alphabet and number.
-Include both the small and capital case letters.
-If the password entered is not valid, the program should give the user another two (2) chances to type the correct password before quitting.
-If the password doesn’t comply to any of the above conditions, then the script should report it as a “Weak Password” otherwise display a “Strong Password”
3. Write a shell script to print a number in reverse order. It should support the following requirements. The script should accept the input from the command line. If you don’t input any data, then display an error message to execute the script correctly.
Possible steps involved in the test script.
1. Suppose the input number is n.
2. Set reverse and single digit to 0 (i.e. rev=0, sd=0).
3. The expression (n % 10) will give the single left most digit i.e. sd.
4. To reverse the number, use this expression <rev * 10 + sd>.
5. Decrease the input number (n) by 1.
6. If n is greater than 0, then go to step no. 3. Else, execute the step no. 7.
7. Print the result.
Note: sd and rev are just variable names. However, you don’t have to use these steps as you can come up to your own way of solving the problem.
4. For given numbers 1 to 10, display any number that is less than 5 and stop the loop.
5. For given numbers 1 to 10, display only odd numbers: 1,3,5,7,9.
Explanation / Answer
Adding Extension Script
#!/bin/bash
dir=$1
for file in `ls $1/*`
do
mv $file $file.bak
done
Validate Password Script
# Initializing attempts to 0
i=0
# Checking number of attempts
while [ $i -lt 3 ];
do
# Incrementing attempts
i=$((i+1))
echo "Enter the password"
# Reading password
read password
length="${#password}"
# Checking the length of password
if test $length -ge 8 ; then
# Checking upper case letter in password
echo "$password" | grep -q [A-Z]
if test $? -eq 0 ; then
# Checking lower case letter in password
echo "$password" | grep -q [a-z]
if test $? -eq 0 ; then
echo "Strong password"
i=3
else
echo "Weak Password - Lower Case Characters are missing from Password"
fi
else
echo "Weak Password - Upper Case Characters are missing from Password"
fi
else
echo "Weak Password - Password length is less than 8 characters"
fi
done
Reverse Script
# Accepting the input from command line
n=$1
# Initializing sd and rev to 0
sd=0
rev=0
# Calculating the number of digits in n
nlength=${#n}
# Checking the number of digits greater than 0
while [ $nlength -gt 0 ];
do
sd=$((n%10))
rev=$(((rev*10) + sd))
n=$((n-1))
nlength=$((nlength-1))
done
# Printing rev
echo $rev
Display Any Number Less Than 5 Script
# Generating a RANDOM Number
RANDOM=$$
# Considering only the last digit
n=$(($RANDOM%10))
# Comparing if it is greater than 10
while [ $n -ge 10 ]
do
# Generating another RANDOM number and cosidering only the last digit
n=$(($RANDOM%10))
done
# Priting
echo $n
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.