Linux Shell Programming Basics
I have been reading Wrox Beginning Linux Programming this week. The book mainly teaches how to programming in GNU/Linux using C. Since I’m learning the basics of C, I thought I could understand some of the materials in this book. In Chapter 2, it talks about shell programming and how does it relate to C programming in GNU/Linux. I wrote the following code which is based on the example code in Chapter 2:
#!/bin/sh # This file looks through all the files in the current # directory for the input string, and then prints the name of # those files to the standard output. echo "" echo Enter the lookup value: read input echo "" for file in * do if [ -f $file ] then if grep -q $input $file then echo $file fi fi done echo "" exit 0
And the screenshot below is the output:
Click the image to enlarge
Note that the read command is similar to the scanf function in C. They are used to let user input value. Also note that this script would take a moment to complete depending on the size of each file where the script file is in.
If you have this book, the above script is similar to the example provided in the Chapter 2 of the book, except I removed static input value (POSIX) and added an user input field using read command and several blank lines to make the output more cleaner.
