Question

In: Computer Science

In bash Write a script which receives a list of parameters. If no parameters are received,...

In bash

Write a script which receives a list of parameters. If no parameters are received, output an error message. Otherwise, iterate through the list and, using a case statement, test each item in the list to see if it starts with a capital letter, lower case letter, digit or other. Count each type and output at the end the total for each category (capital, lower case, digit, other).

Solutions

Expert Solution

  1. use the $# to check if count of parameters passed are 0, display an error and exit.
  2. otherwise loop through all parameters passed.
    1. use the first letter from each parameter in case statement
    2. if letter matches with digit, increment the digit count
    3. if letter matches with lowercase, increment the lowercase count
    4. if letter matches with uppercase, increment the uppercase count
    5. if letter matches with any other, increment the any other count
  3. echo the digit count, lowercase, uppercase and any other count

CODE:
# display error if no parameter passed and exit
if [ $# -eq 0 ]
then
echo "Error. No parameter received."
exit;
fi

digit=0
lowercase=0
uppercase=0
anyother=0

# iterate over parameters provided on command line
for item in "$@"
do
# extract first character and use it in case statement
case ${item:0:1} in
# matches if first letter is digit
[0-9])
# increment digit count
digit=$((digit + 1))
;;
# matches if firdt letter is uppercase
[[:upper:]])
# increment uppercase count
uppercase=$((uppercase + 1))
;;
# matches if first letter is lowercase
[[:lower:]])
# increment lowercase count
lowercase=$((lowercase + 1))
;;
# matches if starts with any other letter
*)
# increment any other count
anyother=$((anyother + 1))
;;
esac
done

echo "Digits: $digit"
echo "Lowercase: $lowercase"
echo "Uppercase: $uppercase"
echo "Other: $anyother"

OUTPUT:


Related Solutions

Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result. Submit your program as LastName_FirstName.sh file.
Create a bash script that takes numbers as parameters, calculates sum and prints the result. If...
Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Fully Functional Script written in BASH In this section, you will write a fully functional script...
Fully Functional Script written in BASH In this section, you will write a fully functional script to help your manager with an important project that will build upon what you learned from your script work in Milestone Four. After you have written the script, you will execute it to generate a report for all three users (once for Bob, once for Henry, and once for Frank). You should have three unique generated reports in your ~/scripts directory (or more if...
⦁   Write a Bash script that prompts for user input and reads a string of text...
⦁   Write a Bash script that prompts for user input and reads a string of text from the user. If the user enters a no null (no empty) string, the script should prompt the user to re-enter again the string; otherwise should display the string entered “. ⦁   Given an array of the following four integers (3, 5, 13, 14); write a Bash script to display the second and all elements in the array.    ⦁   Write a short Bas...
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all...
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all the files in a directory. b. Write a command sequence or a script to insert a line “GHIJKLM” at every 10th line of a file? c. Write a bash command or script to find all the files modified in less than 5 days and print the record count of each.
Bash Script Write a script using while-do-done loop to convert the kilometers to miles. - Ask...
Bash Script Write a script using while-do-done loop to convert the kilometers to miles. - Ask the user to input the number of kilometers they need to travel. - Display the number of equivalent miles for the number. Use formula, Kilometers = miles/0.62137 - Every time the loop runs, it should ask the user if they want to continue, if user enters “Y” or “y”, then the loop runs again, if user enters “N” or “n”, then stop the loop...
Write a bash script that... create new user ./finalProject user if a new user is to...
Write a bash script that... create new user ./finalProject user if a new user is to be created ask for the new users information and use it when creating the new user add a new printer ./finalProject printer ask anything you need in order to create the new printer (I.e. name) permissions ./finalProject permissions ask what document and permissions the user wants restarting the computer ./finalProject restart
Write a bash script named q1f.sh that will edit the PATH environment variable to include the...
Write a bash script named q1f.sh that will edit the PATH environment variable to include the sourcefiles directory in your home directory and make the new variable global. Please show picture of output.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT