Question

In: Computer Science

(10pts) Write a shell script to display your name to the screen. (10pts) Write a shell...

  1. (10pts) Write a shell script to display your name to the screen.
  2. (10pts) Write a shell script to take a directory as an argument and display the contents of that directory
  3. (10pts) Write a shell script that takes any command as a parameter and displays help on that command (e.g. the result of the execution of man <command>).
  4. (10pts) Write a shell script that requires two file names as parameters and copies content of one file into another without asking any questions. The output file should contain a note that it is a copy, name of the original file, current date and time after the content. (lookup 'cp' command)
  5. (10pts) Write a shell script that requires a file name as a parameter. Prompts the user if the file should be deleted. If the answer is 'Y' or 'y' and the file exists, it should be deleted. (lookup 'rm' command)
  6. (10pts) Write a shell script that asks the user to enter a number, prints that number and prints whether that number is odd or even.
  7. (10pts) Write a shell script that prompts for a year of birth. The script should output "Minor" if the user is younger than 18. Otherwise, the script should output "Adult" if the user is younger than 65, "Retired" otherwise.
  8. (10pts) Write a shell script with an input parameter of a valid file extension. Then the script should print the number of files with that extension in the current directory.
  9. (10pts) Write a shell script that takes no more than 5 numbers as parameters and prints the largest of them.

Solutions

Expert Solution

1)  Write a shell script to display your name to the screen.

echo "$USER"

---------------------------------------

2) Write a shell script to take a directory as an argument and display the contents of that directory

ls -F ("Dir name)

----------------------------------------

3)  Write a shell script that takes any command as a parameter and displays help on that command

help (command)

-----------------------------------------

4)  Write a shell script that requires two file names as parameters and copies content of one file into another without asking any questions. The output file should contain a note that it is a copy, name of the original file, current date and time after the content

cp file1 file2

-------------------------------------------

5)  Write a shell script that requires a file name as a parameter. Prompts the user if the file should be deleted. If the answer is 'Y' or 'y' and the file exists, it should be deleted. (lookup 'rm' command)

rm -i filename ( i is for inquire)

----------------------------------------------

6)  Write a shell script that asks the user to enter a number, prints that number and prints whether that number is odd or even.

echo -n "Enter numnber : "
read n

rem=$(( $n % 2 ))

if [ $rem -eq 0 ]
then
echo "$n is even number"
else
echo "$n is odd number"
fi

----------------------------------------------

7) Write a shell script that prompts for a year of birth. The script should output "Minor" if the user is younger than 18. Otherwise, the script should output "Adult" if the user is younger than 65, "Retired" otherwise

echo -n "Enter your DOB [ddmmyyyy] : "
read n
d=`echo $n | cut -c1-2`
m=`echo $n | cut -c3-4`
y=`echo $n | cut -c5-8`
yy=`date "+%Y"`
mm=`date "+%m"`
dd=`date "+%d"`
if [ $y -le $yy ]
then
yyy=`expr $yy - $y`
mmm=`expr $mm - $m`
if [ $m -gt $mm ]
then
yyy=`expr $yyy - 1`
mmm=`expr $mmm + 12`
fi
if [ $d -gt $dd ]
then
ddd=`expr $d - $dd`
ddd=`expr 31 - $ddd`
else
ddd=`expr $dd - $d`
fi
fi
if [$yyy -lt 18]
echo -n "Minor"
fi
if [$yyy -ge 18 && $yyy -lt 65]
echo -n "Adult"
fi
if [$yyy > 65]
echo -n "Retired"
fi

-------------------------------------------------

8) Write a shell script with an input parameter of a valid file extension. Then the script should print the number of files with that extension in the current directory.

ls -lR /path/to/dir/*("extension) | wc -l 

here extension is .mp3,.jpg , .doc etc

---------------------------------------------

9) Write a shell script that takes no more than 5 numbers as parameters and prints the largest of them.

read -p "Enter 1st Number:" num1
read -p "Enter 2nd Number:" num2
read -p "Enter 3rd Number:" num3
read -p "Enter 4th Number:" num4
read -p "Enter 5th Number:" num5
if[ [ num1 -gt num2 ] && [ num1 -gt num2 ] && [ num1 -gt num3 ] && [ num1 -gt num4 ] && [ num1 -gt num5 ]] ; then
      echo "$num1 is a Greatest Number"
elif[ [ num2 -gt num3 ] && [ num2 -gt num3 ] && [ num2 -gt num4 ] && [ num2 -gt num5 ]] ; then
     echo "$num2 is a Greatest Number"
elif[ [ num3 -gt num4 ] && [ num3 -gt num5 ] ] ; then  
     echo "$num3 is a Greatest Number"
elif[ num4 -gt num5 ] ; then  
     echo "$num4 is a Greatest Number"
else
     echo "$num5 is a Greatest Number"
fi

Related Solutions

Write a script to display the following patterns on the screen. Number of rows and columns...
Write a script to display the following patterns on the screen. Number of rows and columns are taken from the command arguments; if they are missing, set default to 3 (rows) and 4 (columns). Hint: you will use a nested loop. **** **** **** a) Display the source code in an editor (#4-11) b) Execute your script in the terminal, and display the command and the result (#4-12)
Write a brief shell script that will take in a specific file name, prompt the user...
Write a brief shell script that will take in a specific file name, prompt the user whether they would like to gzip, bzip2, or xz compress the file. Depending on response, the script then ought to compress the provided file with the corresponding method
Write a bash shell script that takes exactly one argument, a file name. If the number...
Write a bash shell script that takes exactly one argument, a file name. If the number of arguments is more or less than one, print a usage message. If the argument is not a file name, print another message. For the given file, print on the screen its content.
Write a bash script that will allow you to: Read in your Your name Course name...
Write a bash script that will allow you to: Read in your Your name Course name and Instructor’s name Then prompt the user to enter two numbers and determine which number is greater. Ex: If 10 is entered for the first number and 3 for the second, you should output Your name Course name Instructor’s name 10 is greater than 3. If 33 is entered for the first number and 100 for the second, you shou output Your name Course...
Write a complete shell script that first asks the user to enter a URL. The script...
Write a complete shell script that first asks the user to enter a URL. The script should read the URL into a variable named url. The shell script should then retrieve the file associated with the URL by using the curl command. The output of the curl command should be redirected to a file named html_file. The shell script should then use the grep command to search the file named html_file for the word manhattan. Finally, the shell script should...
Write a shell script (to run on the Bourne shell) called cp2.sh to copy all the...
Write a shell script (to run on the Bourne shell) called cp2.sh to copy all the files from two named directories into a new third directory. Timestamps must be preserved while copying files from one directory into another. Task Requirements Three directory names must be supplied as arguments to your script when it is executed by the user. In the beginning of your script you need to check that the first two directory names provided (e.g. dir1 and dir2) exist...
Name your source code file sh.c Write your own simple shell. Your shell should prompt the...
Name your source code file sh.c Write your own simple shell. Your shell should prompt the user for a command, run it, then prompt the user for their next command. If the user types "exit", then the shell should terminate. The shell should ignore Ctrl-C. Inside the attached parse.h header file, there is a parse function that you can use to split up the command line into separate strings. Recall that execvp accepts two arguments: the name of the command,...
python Write a program that prints your name 100 times to the screen. Write a function...
python Write a program that prints your name 100 times to the screen. Write a function that takes a string s and an integer n as parameters and prints the string s a total of n times (once per line). Write a for loop that prints all the integers from 3141 to 5926, skipping every other one. Write a for loop that prints every 5th integer from 5926 down to 3141. Write a program (using a for loop) to print...
Using c++, write a program that will display your name as a void function then will...
Using c++, write a program that will display your name as a void function then will perform the following by user-defined functions: a. to compute for the sum of two numbers (n1, n2) using function.
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string expressed as -rwxrwxrwx and prints out whether or not theobject is a directory, file or link. The string is read in from standard input. Convert String (Part 2) Modify the script so its able to print out the octal permission which the string represents along with the file type. This is in addition to part 1. Convert String (Part 3) For the script in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT