- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Shell Script Meaning
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2005 07:56 PM
тАО11-07-2005 07:56 PM
Shell Script Meaning
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/:.*//"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2005 08:08 PM
тАО11-07-2005 08:08 PM
Re: Shell Script Meaning
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2005 08:13 PM
тАО11-07-2005 08:13 PM
Re: Shell Script Meaning
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2005 08:21 PM
тАО11-07-2005 08:21 PM
Re: Shell Script Meaning
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2005 08:32 PM
тАО11-07-2005 08:32 PM
Re: Shell Script Meaning
http://www.faqs.org/docs/artu/index.html
also see the attached file.
Awadhesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2005 10:07 PM
тАО11-07-2005 10:07 PM
Re: Shell Script Meaning
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-07-2005 10:14 PM
тАО11-07-2005 10:14 PM
Re: Shell Script Meaning
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-08-2005 09:27 PM
тАО11-08-2005 09:27 PM
Re: Shell Script Meaning
and your calls will be more productive.
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-09-2005 12:20 AM
тАО11-09-2005 12:20 AM
Re: Shell Script Meaning
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-09-2005 05:10 AM
тАО11-09-2005 05:10 AM