Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Single UNIX command to display users and to count them Post 302739623 by bakunin on Tuesday 4th of December 2012 02:54:37 PM
Old 12-04-2012
Actually, these are very good questions and i am glad that you asked. It is always much more satisfying to teach someone willing to learn.

Quote:
Originally Posted by sreyan32
1) In the following code-:

Code:
$(( $(w| wc -l) - 2 ))

Why have you used two opening and two closing brackets for the outer expression and only one opening and one closing for the inner expression that is-:

Code:
$(w| wc -l)

I tried it with two opening and closing brackets for both inner and outer expression and I keep getting errors. Same thing if I try with one bracket for both inner and outer expressions.Why does it only work like the way that you have specified ?
These are different language constructs. First the "$(( ... ))": this means: replace the numeric expression inside by its outcome. For instance you could write

Code:
foo -x $(( 5 - 3 + 1 ))

and this would be the same as

Code:
foo -x 3

The shell doesn't differentiate between numeric and string variables, at least not in the sense a high-level language does. Consider the following lines:

Code:
z=0
x="abc5"
x="${x#???}"    # this chops off the first 3 characters, leaving "5"
(( z = x * 5 ))
echo $z

I started the variable as string, then manipulated it like a string and finally, because its value was numeric, used it like an integer. So, if you want to make clear to the shell that the following is a numeric operation, you signify this by "(( ... ))". You can add "$" in front to replace it with its outcome in another line.

The other device, "$( ... )" is a subshell: it runs the commands inside in a separate shell and (the "$" in front) replaces it with its outcome (which could well be a string, it doesn't have to be numeric). It is the same device which is used at the end of the command:

Code:
>(cut -d\  -f1|uniq | wc -l >/dev/stderr )

Which means: run all the commands inside in a separate shell, directing something to the "stdin" input device of this shell (the ">").

Quote:
Originally Posted by sreyan32
2) When we use tee the stream is duplicated and by default the first stream is always printed out. Am I right in assuming this ? Or is there a way to control the output of both the divided streams?
Yes, there is: the is the "-a" switch of "tee". I suggest you enter "man tee" to read its man page, which usually will also contain some examples.

Quote:
Originally Posted by sreyan32
3)The duplicate stream that tee creates is also an output stream. And we redirect that second duplicate stream to the following operations-:

Code:
(cut -d\  -f1|uniq | wc -l >/dev/stderr )

But then if there is already an output stream then why are we redirecting the output to the error stream ?
First, the redirection to "stderr" is only happening with the very last commands output. Before this, it is always "stdout" to "stdin". In fact the pipe symbol "|" means: connect the "stdout" of the first process to "stdin" of the second process.

Whe i told you about UNIX processes being like garden hoses, this was a simplified picture of reality: in fact all the UNIX commands are more like "Y-shaped garden hoses". They have a "stdin" where data are puored in, but 2 outputs (one "stdout" for normal output, the other "stderr" usually used for error messages), which can both be directed anywhere separately. For instance:
connect the stdout
Code:
ls -l /etc/passwd /some/file/blabla

will produce an error message because "/some/file/blabla" doesn't exist. This error message is going through "stderr" and usually it lands on the screen because both "stdin" and "stderr" are both pointing to the same device per default. That in fact they are from different sources is easy to see once we redirect one of these sources away:

Code:
ls -l /etc/passwd /some/file/blabla > /dev/null
ls -l /etc/passwd /some/file/blabla 2> /dev/null

Notice the difference in output. "/dev/null" is simply the catch-all device. It works like a black hole: whatever goes in is lost, out comes nothing.

Now, to make sure both output streams are separated from each other and are displayed one after the other (not mixed up) it is a good idea to let the one output go to "stdout", the other to "stderr". There are also some intricate differences (buffered versus unbuffered output) i don't think you will grasp for now. I hope you understand that i forego the explanation of some very advanced stuff to a newbie. Still, it is impressing that you came up with this very good question.

Quote:
Originally Posted by sreyan32
By the way do you know what the following command does-:
Code:
kill $!

First, "kill" is used to send signals to a process. "kill" is a regular command and has a man page i suggest you read. It takes a process number as an argument, to which it sends a signal. As there is no signal specified "kill" will send its default signal, which is "TERM" - it asks the process to close down. "$!" is a special variable maintained by the shell and holds the process ID of the last process started in background. So, this command terminates the last started background process.

I hope this helps.

bakunin
These 3 Users Gave Thanks to bakunin For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use more than one MPE command STREAM with Unix command in a single shell?

Hello, I have problem in writing the shell script involving MPE command STREAM related to HP-UX and Unix command. Script is sh "nlshCMD 'STREAM <job name1>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name2>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name3>' |... (0 Replies)
Discussion started by: bosskr
0 Replies

2. HP-UX

How to use more than one MPE command STREAM with Unix command in a single shell?

Hello, I have problem in writing the shell script involving MPE command STREAM related to HP-UX and Unix command. Script is sh "nlshCMD 'STREAM <job name1>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name2>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name3>' |... (1 Reply)
Discussion started by: bosskr
1 Replies

3. UNIX for Dummies Questions & Answers

run command Unix on a single line

Hi everybody.. I need to enter in bash mode and then run a command and this just in a single command line. I tried : "bash ^M| somecommand" but nothing.. How do I do to simulate the return button just right after the bash command ? Thanks.. (8 Replies)
Discussion started by: Riddick61
8 Replies

4. Shell Programming and Scripting

Need a Command To display "echo command value in loop" in single line.

Hi I want to display "echo command value in loop" in single line. My requirement is to show the input file (test_1.txt) like the output file (test_2.txt) given below. Input file :test_1.txt a1|b1|4|5 a1|b1|42|9 a2|b2|32|25 a1|b1|2|5 a3|b3|4|8 a2|b2|14|6 Output file:test_2.txt... (2 Replies)
Discussion started by: sakthifire
2 Replies

5. UNIX for Dummies Questions & Answers

Re: How To Use UNIQ UNIX Command On single Column

Hi , Can You Please let Know How use unix uniq command on a single column for deleting records from file with Below Structure.Pipe Delimter File . Source Name | Account_Id A | 101 B... (2 Replies)
Discussion started by: anudeepkumar123
2 Replies

6. Shell Programming and Scripting

History of all the users in single file with command , date . time , ip and user

HTML Code archive_history() { HISTORYOLD=${HISTFILE}.archive CURTIME=`date` CURTTY=`tty` IP=$(echo $SSH_CLIENT | awk '{print $1}') if ; then echo "#-${HOSTNAME}-- ${CURBASHDATE} - ${CURTIME} ($CURTTY) ${USER} ${IP}----" >> $HISTORYOLD history... (2 Replies)
Discussion started by: rehantayyab82
2 Replies

7. Shell Programming and Scripting

History of all the users in single file with command , date . time , ip and user

HTML Code: archive_history() { HISTORYOLD=${HISTFILE}.archive CURTIME=`date` CURTTY=`tty` IP=$(echo $SSH_CLIENT | awk '{print $1}') if ; then echo "#-${HOSTNAME}-- ${CURBASHDATE} - ${CURTIME} ($CURTTY) ${USER} ${IP}----" >> $HISTORYOLD history... (0 Replies)
Discussion started by: rehantayyab82
0 Replies

8. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

9. UNIX for Beginners Questions & Answers

Get count of multiple word in single command

Hello Experts, I have a log file that contains 4 different type of exception : 1- Exception 2- Fatal 3- Error 4- Exec My requirement is to find count of each type of exception, i tried using combination of -E and -C but that doesn't seems to be working : grep -ec 'Exception' -ec... (4 Replies)
Discussion started by: mukulverma2408
4 Replies

10. UNIX for Beginners Questions & Answers

How to find the count of IP addresses that belong to different subnets and display the count?

Hi, I have a file with a list of bunch of IP addresses from different VLAN's . I am trying to find the list the number of each vlan occurence in the output Here is how my file looks like 1.1.1.1 1.1.1.2 1.1.1.3 1.1.2.1 1.1.2.2 1.1.3.1 1.1.3.2 1.1.3.3 1.1.3.4 So what I am trying... (2 Replies)
Discussion started by: new2prog
2 Replies
All times are GMT -4. The time now is 05:20 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy