Display The Commands Used To Create A Script
Display The Commands Used To Do The Following Create A Script That
Display the command(s) used to do the following: create a script that uses case logic to have someone guess your favorite sandwich, such as tuna. Show results.
Display the command(s) used to do the following: display the contents of .bashrc file. Next, use the vi editor to edit that file and put in an alias so that you see a long file listing of a directory every time you type list. Show results.
Display the command(s) used to do the following: use a command to simulate how you would troubleshoot a problem with the sandwich script you created in the question above. Show results. What is wrong with the following lines of code? While [ “$value†= “100†; do Echo “That’s a large number.†read value Fi There does not need to be a [ While and echo are capitalized It only works on the number 100
Display the command(s) used to do the following: write a script that creates the following menu using a while loop and tput command: Soup Menu ========== (t)omato (b)ean (s)quash Select a soup … (q) to quit Show results.
What are the exit statuses of the test command discussed in this chapter and what do they mean?
How can you view the exit status results of the test command directly from the command line? Show results.
Display the command(s) used to do the following: create a variable called mem_size and set its contents to 1024. Next, use the test command to determine if the contents of mem_size is less than or equal to 512. Show results.
Display the command(s) used to do the following: set your shell from the command line to be the bash shell. Then, use the echo command to verify the contents of the shell variable. What is now contained in the shell variable? Use the test command to evaluate whether the shell variable contains a reference to the bash shell and use the echo command to determine the result. (Note that this provides one way to verify from within a script that the script user is set up to use the bash shell.). Show results.
Display the command(s) used to do the following: make your home directory your current working directory. Use the vi or Emacs editor to create the veg_choice script below. Enter a line in that script to ensure the script uses the bash shell. Run veg_choice to ensure your change works properly. Show results.
echo -n “What is your favorite vegetable?†read veg_name if test $veg_name = “broccoli†then echo “Broccoli is a healthy choice.†else if [ “$veg_name†= “carrots†] then echo “Carrots are great for you.†else echo “Don’t forget to eat your broccoli also.†fi fi Edit the veg_choice script again but, this time, change the line: if [ “$veg_name†= “carrots†] to use the test command. Show results.
Display the command(s) used to do the following: create a shell variable that outputs the calendar for the current month (call it CALNOW). How could you set up CALNOW so that it works every time you log in using the bash shell? Show results.
Display the command(s) used to do the following: in your source directory, write a script called “them†in which you create a function called whoisthere that displays a listing of who is logged in and displays the column headings for the information displayed. Make the contents of your “them†script resident in memory and test your whoisthere function. Modify your whoisthere function so that you can enter “These are the folks logged in:†as an argument to appear before you list who is logged into the system. What actions do you take next to use the whoisthere function with your modifications? How can you set up your new whoisthere function so that it can be run each time you log in using the bash script? Show results.
Troubleshoot the problems with the following script (there are 6 errors/corrections. Describe them below): #=============================================== Script Name: record_entry By: TRJackson #=============================================== looptrack=y while [ “$looptrack†= 1 ] do echo -n “Type in the account number:†read account echo -n “Type the first and last name:†; read full_name echo -n “Type the age:†red age echo -n “Enter another record?†; read looptrack finish
Paper For Above instruction
This document provides a comprehensive overview of various fundamental Linux commands and scripting techniques, demonstrating how to automate tasks and troubleshoot common issues within a Unix-like environment. The commands involve scripting with conditional logic, manipulating configuration files, setting environment variables, creating menu-driven interfaces, checking process statuses, and debugging scripts to ensure accurate execution.
To initiate, a script utilizing case logic is employed to simulate a guessing game for one's favorite sandwich, such as tuna. The basic Bash script uses a case statement that prompts the user to guess and responds accordingly. The key command structure included is:
#!/bin/bash
echo "Guess my favorite sandwich:"
read guess
case "$guess" in
tuna)
echo "Correct! Tuna is my favorite sandwich.";;
turkey)
echo "Nope, it’s not turkey.";;
ham)
echo "Nope, it’s ham.";;
*)
echo "Try again.";;
esac
Next, to display the contents of the .bashrc file, the command:
cat ~/.bashrc
To add an alias within the .bashrc using the vi editor that shows a long directory list with 'ls -l' when typing 'list', the sequence is:
vi ~/.bashrc
Append the alias at the end:
alias list='ls -l'
Save and exit the editor.
Troubleshooting a script with a syntax or logical error involves examining the script. In this case, the issues with the code:
While [ “$value†= “100†; do Echo “That’s a large number.†read value Fi
include incorrect syntax like the capitalization of commands, incorrect brackets, quotation marks, and improper use of control structures. The correct structure should be:
while [ "$value" -eq 100 ]; do
echo "That's a large number."
read value
done
For creating a menu using while loop and 'tput' command, a script similar to:
#!/bin/bash
while true; do
tput clear
echo "Soup Menu"
echo "=========="
echo "(t)omato"
echo "(b)ean"
echo "(s)quash"
echo "Select a soup … (q) to quit"
read -n 1 choice
case "$choice" in
t) echo "Tomato soup selected.";;
b) echo "Bean soup selected.";;
s) echo "Squash soup selected.";;
q) break;;
*) echo "Invalid choice.";;
esac
read -p "Press Enter to continue..."
done
Exit statuses of the 'test' command are typically 0 for success (condition is true) and 1 for failure (condition is false). These statuses can be viewed directly by checking the special variable '$?' immediately after executing a test using:
test condition
echo $?
To create a variable 'mem_size' with value 1024 and test if it's less than or equal to 512, the commands are:
mem_size=1024
if [ "$mem_size" -le 512 ]; then
echo "Memory size is less than or equal to 512."
else
echo "Memory size exceeds 512."
fi
Changing the shell from the command line to bash involves:
chsh -s /bin/bash
To verify the shell variable:
echo "$SHELL"
To check if the shell is bash:
if [ "$SHELL" = "/bin/bash" ]; then
echo "Using Bash shell."
else
echo "Not Bash shell."
fi
Changing directory to home:
cd ~
Creating 'veg_choice' script with shebang line:
#!/bin/bash
echo -n "What is your favorite vegetable? "
read veg_name
if [ "$veg_name" = "broccoli" ]; then
echo "Broccoli is a healthy choice."
elif [ "$veg_name" = "carrots" ]; then
echo "Carrots are great for you."
else
echo "Don't forget to eat your broccoli also."
fi
Modifying the 'veg_choice' script to use the 'test' command:
if test "$veg_name" = "carrots"; then
echo "Carrots are great for you."
fi
Creating the 'CALNOW' variable to output current month's calendar:
CALNOW=$(cal)
To automatically set 'CALNOW' on login, add the above line to your ~/.bash_profile or ~/.bashrc file.
Writing a script 'them' with a function 'whoisthere':
#!/bin/bash
function whoisthere() {
echo "These are the folks logged in:"
who | column -t
}
export -f whoisthere
To use in current session:
source ./them
Then call:
whoisthere
To modify 'whoisthere' to accept an argument:
function whoisthere() {
echo "$1"
who | column -t
}
Using 'source' command to make the function available in login:
source ./them
Now you can call:
whoisthere "These are the folks logged in:"
Troubleshooting the provided script with six errors involves correcting syntax, control structure, and variable usage issues:
- Replace curly quotes with standard ASCII quotes.
- Change 'While' to lowercase 'while'.
- Fix 'Echo' to lowercase 'echo'.
- Correct 'red' to 'read' for reading the 'age' variable.
- Replace 'Fi' with 'done' to close the loop properly.
- Add a 'then' after 'if' conditions and proper 'elif' syntax.
Corrected script excerpt:
#===============================================
Script Name: record_entry
By: TRJackson
===============================================
looptrack=y
while [ "$looptrack" = 1 ]; do
echo -n "Type in the account number: "
read account
echo -n "Type the first and last name: "
read full_name
echo -n "Type the age: "
read age
echo -n "Enter another record? (1 to continue, 0 to stop): "
read looptrack
done