Vous êtes sur la page 1sur 2

Practice with Korn Shell Scripting

Easy
1. Write a Korn shell script mygreet that prompts the user to enter their name, greets the user by name and says your current directory is _____. Hints: User input, page 657 pwd command displays current working directory. Inside a script, you can either enclose a command in `` or $(), for example: `pwd` or $(pwd) 2. Write a script 3args that takes 3 command like arguments from the user, tells the user the number of arguments he/she entered, and echoes back the arguments. Hints: page 666 Sample output: ./3args x y z You entered 3 arguments The first argument is x The second argument is y The third argument is z Medium 3. Change the 3args script you wrote so that is validates that the user has entered exactly 3 arguments. Call the new script 3args2. 4. Write a script called dirsearch that will search all files in the current directory and display all the files that are executable. Hints: Test to see if file is executable, page 671 for loop page 688 More Difficult 5. Write a script mysearch that will prompt the user to enter a filename. First check to make sure the file exists. Next, prompt the user to enter a pattern to search for. Finally search the contents of the file for the supplied pattern. Print the matching lines, or let the user know that no matching lines were found. Sample output: ./mysearch Enter a filename: names Enter a pattern to search for: Nancy Lines matching the pattern Nancy in the file names are: Nancy Drew Hints: Korn shell print command: page 624 User input: page 657 IF Then for file existence: page 673 Search for file contents: grep chapter 4 To tell user if no lines were found, page 85 check for exit status of 1

6. Change the mysearch script so that the filename and pattern are specified on the command line. Make sure the user enters 2 command line arguments. If not, display a USAGE message. Call the new script mysearch2 Sample output: ./mysearch2 USAGE: mysearch2 filename pattern ./mysearch2 names Nancy Lines matching the pattern Nancy in the file names are: Nancy Drew 7. Write a script multiply to calculate and print out the multiplication tables from 1 to 12. There are a number of different ways to accomplish this, but make sure you are calculating the products and not just printing out the answers. Sample output $ ./multiply 1 2 2 4 3 6 4 8 5 10 6 12 7 14 8 16 9 18 10 20 11 22 12 24

3 6 9 12 15 18 21 24 27 30 33 36

4 8 12 16 20 24 28 32 36 40 44 48

5 10 15 20 25 30 35 40 45 50 55 60

6 12 18 24 30 36 42 48 54 60 66 72

7 14 21 28 35 42 49 56 63 70 77 84

8 16 24 32 40 48 56 64 72 80 88 96

9 18 27 36 45 54 63 72 81 90 99 108

10 20 30 40 50 60 70 80 90 100 110 120

11 22 33 44 55 66 77 88 99 110 121 132

12 24 36 48 60 72 84 96 108 120 132 144

Hints: Arithmetic: page 660 For loop: page 687 While loop: 690 8. Write a script that accepts 1 argument, a username and the script check to see if the user is currenly logged in and displays an appropriate message. Sample output: ./logincheck espencer espencer is currently logged in ./logincheck smith Sorry, smith is not logged in right now Hints: who command shows a list of currently logged in users something on the order of who | grep $user

Vous aimerez peut-être aussi