Unix uses a system of Pipe where and Output of one Shell command can be used as an Input for other Shell command. You can compare it with multiple Garden Hose pipes tied to each other in a way lie water coming out from one end is entering the other hose pipe. How you want to use that output depends upon you. Like in the above case we need a total count of people logged in and so we apply pipe to our who command like this :
who | wc -l
|
Where :
wc: Word Count and since we want to count the lines as each line tells us the user logged in so we use and option of word count i.e. -l .
|
Unix shell give us another beautiful feature of using the options to further tweak the output we want to see. The point is, we need a direct answer to our question rather than all the options which a Unix command is giving us and so we use options and filters etc. And when you run the above command in terminal you'll see the output as number( Number of Users logged into you system) So in above case it will be 1.
Other Shell Command we will use is:
date: It's quite clear from the name itself what it will display and its output without any filter looks like this:
Sun Jul 8 09:06:19 IST 2012
Now lets combine the above two shell command and put them in a script file so that you don't have to run them individually. We will create the file first and you can create that through Unix Shell itself using cat command. Its executed as:
cat > <file name>
Name the file whatever you want and run the command like:
cat > MyFirstShellScrpit.sh
|
I have put .sh in the end for my understanding that its a shell scrpit file. You can choose not to put that extension but i suggest you should do it for future easy reference. After running the above command you will see that terminal is just showing a blinking cursor which means its waiting for you to add the contents to the file you just created. So we will enter the commands we discussed earlier and its like this :
cat > MyFirstShellScrpit.sh |
who | wc - l |
date |
^d |
^d is used in Unix Shell to tell that its the end of file and please go back to the prompt. And Bingo your first shell script file is saved and ready to run. But before running a shell script we need to make it executable also like we have and .exe or .bat file in windows. So type in Terminal
chmod +x MyFirstShellScrpit.sh
|
Where : chmod is a Unix command which tells to change the mode of the file. There are three modes of a file which are read, write and Execute written as r, w, x and + means that I want to add this mode to the file( so - means remove that mode). So the above file will make it executable. We can run our shell script now. And we run it as:
And the output should be:
1 |
Sun Jul8 09:06:19 IST 2012 |
Congratulations. You have just run your first shell script. So every time you feel like checking how many users are currently logged in, just run the script and bingo, count of people with date will be there on your screen.
Lets do one thing more to make this script more useful. You want to track the total number of logins on that day for a particular month so its better to save it in a file also. So we do something like this:
./MyFirstShellScrpit.sh > data.txt
|
(Where > operator will send the output of the shell script to the file data.txt and if that file doesn't exist , it will create it automatically)
You just need to be a bit cautious with it because if the above file exist already it will overwrite all the contents without giving any warning.
|
So how to keep adding the data everyday. For that we use the shell script as:
./MyFirstShellScrpit.sh >> data.txt
|
Where >> means that append my output to the previous output which is there. So check the file data.txt after a some days you will see something like this:
1 | |
Sun Jul 8 09:06:15 IST 2012 |
5 | |
Sun Jul 9 09:07:19 IST 2012 |
4 | |
Sun Jul 10 09:07:19 IST 2012 |
The output will vary as per number of users logged in at the time you checked. So its like you want to track how many people were in office at around 9 a.m. logged in, this basic command will do the purpose.
This is just the basic and you could see the beauty of simple shell script.
No comments:
Post a Comment