1822494 Members
2535 Online
109642 Solutions
New Discussion юеВ

Shell Script Meaning

 
james parker_3
New Member

Shell Script Meaning

Hi:

I am researching for a presentation I have to give tomorrow about UNIX basics: I know nothing about UNIX, and neither does my audience: it is just a basic overview. I wanted them to get an idea of what code looks like, so a techie friend sent me the shell script below, but i cant get hold of him now, but am worried someone will ask me what this means (in basic terms), and want to give a truthful answer: any help? This is the code:

#!/bin/ksh
grep -c $1 * | grep -v ":0$" | sed "s/:.*//"
9 REPLIES 9
Orhan Biyiklioglu
Respected Contributor

Re: Shell Script Meaning

$1 is the first argument to your script.

If you invoke it like

#myscript orhan
grep -c $1 * will search all the files in the current directory for the occurence of 'orhan' and print out the number of occurences like

file1:0
file2:1
file3:5

second part

grep -v ":0$" will get rid of the ones ending with :0 (the ones with zero occurences)

file2:1
file3:5

the last part will delete the :coount part at the end.

so the final output will be

file2
file3

hth
Arunvijai_4
Honored Contributor

Re: Shell Script Meaning

Here it is,

grep -c prints only a count of matching lines
$1 - 1st Argument to your script in command line.
grep -v ":0$" | sed "s/:.*//" will remove :0.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Senthil Prabu.S_1
Trusted Contributor

Re: Shell Script Meaning

Hi,
This command means,
first, it look for all files in current directory and print only the lines that matches pattern, which will be your first argument of the script. And also, it exclude those lines in files which end with ":0". And finally, sed replacse ":." with nothing.



One man's "magic" is another man's engineering. "Supernatural" is a null word.
AwadheshPandey
Honored Contributor

Re: Shell Script Meaning

james check these links usefull to u
http://www.faqs.org/docs/artu/index.html
also see the attached file.

Awadhesh
It's kind of fun to do the impossible
john korterman
Honored Contributor

Re: Shell Script Meaning

Hi James,

The idea of the script is basically to search for occurrances of a certain text string in a number of files (I guess).


The script consists of two lines:

#!/bin/ksh
which is a reference to the shell used for interpreting the commands of the following lines (whether or not the shell exists or is obsoleted is a matter open to discussion that we better skip!)

The second line is divided into three parts, each part nicely separated from the other by the pipe characters.
The pipe in unix is used for redirecting output - your friend may have chosen this for demonstrating a very basic unix-feature, namely that output from one command may be used as input for another; in this case the output from the first part is used as input for the second part, whose output is used as input for the third part.
This is possible because of the "silent programming feature" of unix: if execution goes well, you get the plain output, i.e. you are not told that this was an excellent command or the like.


The three parts of the second line can be viewed in isolation
grep -c $1 *
searches for the string specified in $1 in the file(s) matched by the asterisk.
$1 refers to the first parameter specified when executing a script, e.g. if the script name is check_name.sh, an execution could look like this:
$ check_name.sh parker

meaning that you want to search for the string "parker"
The search will be executed in some files.... the asterisk indicates file name expansion and will expand to all files located in the same directory as where the script is located (not very flexible in my opinion, but that is another story).
As mentioned you will only get the number of hits because of the -c option to grep

The output of the first part would show a number of lines, each holding a file name and the number of hits, separated by a colon, e.g.:

file1:0
file2:0
file3:2
file4:5

The next part of the script
grep -v ":0$"

then filters away input lines ending in :0
(in this case the pound sign refers to the end of the line).
The idea of the script is probably that files with zero hits are not interesting
This part thus reduces the above output to this:

file3:2
file4:5


And now the last part of the script
sed "s/:.*//"
removes from each line the colon and whatever appears after the colon. The above output will now look like this:


file3
file4


Apparently the script does not think that you are interested the number of hits you get in each file, only if it is 1 or greater.



regards,
John K.
it would be nice if you always got a second chance
Muthukumar_5
Honored Contributor

Re: Shell Script Meaning

Ok.

To start briefly,

#! -> Shell interpreter used to interpret shell scripting
/bin/ksh -> Shell is going to be used

grep -> It is an utility to search for a string in all files in the current directory (*)

grep -c -> It is used to grep $1 variable string in the current directory files and report how many times, it is repeated.

grep -c $1 * | -> After grepping string, output is given to grep -v ":0$" operation.

grep -v ":0$" -> Used do grep things except :0 at the end.

| sed "s/:.*//" -> It is getting input from grep -v operation and removing all things including :

hth.


Easy to suggest when don't know about the problem!
Arturo Galbiati
Esteemed Contributor

Re: Shell Script Meaning

My suggestion is that you work togher your class trying to discover how the script work. Suggest them to use man grep, man sed
and your calls will be more productive.
HTH,
Art
Steve Post
Trusted Contributor

Re: Shell Script Meaning

May I also suggest to a look into "regular expressions".

That $ in grep -v ":0$"? It pins the search text to the end of the line. That is the string is looking for lines like "xxx:0" but not like "xxxx.0 hi" The difference is the 0 is at the end of the line.


Look at :.* in that sed "s/:.*//"
: really is a colon
. means a character. ANY character.
* means a one or more of the thing before it.
In this case, it is one or more of "any character". So, ".*" really means a bunch of characters. And since there is a colon in front of it, it means "a bunch of characters after a :".

So a line line "hihothere:this is a test"
changes to "hotothere"

james parker_3
New Member

Re: Shell Script Meaning

Thanks for all your help: really appreciated.