LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-05-2003, 11:02 AM   #1
Satriani
Member
 
Registered: Mar 2003
Location: The Netherlands
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418

Rep: Reputation: 30
All output to /dev/null?


Hi all,

Maybe a stupid question, but i cant figure it out...

I have a little script to start X on another display, and want it to start in the background.

Here it is:
Code:
#!/bin/sh

XSRV=$1
XSCREEN=$2

# Check if we are called in with parameters:
# If not, exit the program.
if [ -z $XSRV ]
then
 echo
 echo "  Usage: `basename $0` server_name [display_number]"
 echo "  Valid display numbers are 1, 2 and 3"
 echo "  Where the default display = 1"
 echo
 exit 1
fi


#Check if an optional screen is passed:
if [ -z $XSCREEN ]
then
  XSCREEN=1
fi


# check if X is installed on this machine:

XSTARTER=`which X 2>/dev/null`
if [ -z $XSTARTER ]
then
  echo
  echo "No X-server located in $PATH";
  exit 1
fi

# Check if the second arg is a screenNumber within 1 to 3...

case $XSCREEN in
  [1-3]) CONT=true;;
  *) echo "$XSCREEN is not a valid display number (please use 1,2 or 3)"
     exit 1;;
esac

let newTerm=$XSCREEN+7



echo
echo "   $XSRV will start on VT $newTerm in 3 seconds."
echo "   Program will run in background"
echo "   Press ctrl-c to abort...."
sleep 3

echo Now connecting....

$XSTARTER :$XSCREEN -once -keeptty -quiet -query $XSRV 2>&1 /dev/null &
echo "To enter the new window, press CTRL-ALT-F$newTerm";
As you can see on the last lines, i direct the output to dev null.

However, it keeps showing information about X in the screen what started it. Also, the -keeptty option doesn't seem to work.

I guess you understand the meaning of the script..

How can i stay in the TTY I started from, and suppress all output from X ?

(I still see myself as a newbie, so be clear in your answers please?)

TIA!

Last edited by Satriani; 10-05-2003 at 11:04 AM.
 
Old 10-05-2003, 12:29 PM   #2
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
$XSTARTER :$XSCREEN -once -keeptty -quiet -query $XSRV 2>&1 > /dev/null

you are missing an > here before /dev/null..you put it there and things should be fine
 
Old 10-05-2003, 12:53 PM   #3
Satriani
Member
 
Registered: Mar 2003
Location: The Netherlands
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418

Original Poster
Rep: Reputation: 30
I still have output:
Code:
[beta@laptop bin]# xconnect myotherserver

   myotherserver will connect on VT 8 in 3 seconds.
   Program will run in background
   Press ctrl-c to abort....
Now connecting....
To enter the new window, press CTRL-ALT-F8
[beta@laptop bin]#
XFree86 Version 4.3.0 (Red Hat Linux release: 4.3.0-2)
Release Date: 27 February 2003
X Protocol Version 11, Revision 0, Release 6.6
Build Operating System: Linux 2.4.20-3bigmem i686 [ELF]
Build Date: 27 February 2003
Build Host: porky.devel.redhat.com

        Before reporting problems, check http://www.XFree86.Org/
        to make sure that you have the latest version.
Module Loader present
OS Kernel: Linux version 2.4.20-20.9 (bhcompile@stripples.devel.redhat.com) \
(gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)) #1 Mon Aug 18 11:45:58 \
EDT 2003
Markers: (--) probed, (**) from config file, (==) default setting,
         (++) from command line, (!!) notice, (II) informational,
         (WW) warning, (EE) error, (NI) not implemented, (??) unknown.
(==) Log file: "/var/log/XFree86.1.log", Time: Sun Oct  5 19:51:04 2003
(==) Using config file: "/etc/X11/XF86Config"
    [10f] 320 x 200, 70Hz
    [112] 640 x 480, 60Hz, 72Hz, 75Hz, 85Hz, 100Hz
    [115] 800 x 600, 60Hz, 72Hz, 75Hz, 85Hz, 100Hz
    [118] 1024 x 768, 60Hz, 70Hz, 75Hz, 85Hz, 43Hz, 100Hz
    [11b] 1280 x 1024, 60Hz, 75Hz, 85Hz, 43Hz
    [11e] 640 x 400, 70Hz
    [124] 1600 x 1200, 48Hz, 60Hz, 75Hz, 85Hz
    [134] 320 x 240, 72Hz
    [144] 400 x 300, 72Hz
    [154] 512 x 384, 70Hz
    [175] 720 x 480, 75Hz
    [17a] 720 x 576, 75Hz
and also the -keeptty still doesn't work?

Any other suggestions?

Last edited by Satriani; 10-05-2003 at 12:56 PM.
 
Old 10-05-2003, 08:07 PM   #4
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
i think the Xserver is not allowing its output to be sent to any log file or anyplace else..so you'd have to send all the output of the script to /dev/null like this:
Xconnect <my-server> 2>/dev/null

by this you get what you want and dont get the Xserver logs that are anyway also going to the /var/log/XFree86.1.log file.

as for your keeptty option not working...i am at a loss....

sorry cant be of any more help

i dont know what the keeptty option does..it is for debugging ..so as to keep hold of the calling terminal, right? but what should i be able to see.

i dont know much about Xservers, etal
 
Old 10-06-2003, 07:46 AM   #5
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
i dont know anything about X command line options or -keeptty but i think

> /dev/null 2>&1 & would work much better than 2>&1 > /dev/null &
 
Old 10-07-2003, 11:22 AM   #6
Satriani
Member
 
Registered: Mar 2003
Location: The Netherlands
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418

Original Poster
Rep: Reputation: 30
Can you explain the difference in these options to dev/null ? why is the first one better? (Just to make me understand it?)
 
Old 10-07-2003, 12:04 PM   #7
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33

what kev says works,
what i understand from this is :
i) the output to be redirected is not being sent on 1(stdout) but only on 2(stderr), so if you were to do just a redirection of 2 to the desired file it would work just fine.(the way you want it to).

ii) that >"file" 2>&1 &makes sense because >"file" means redirecting the stdout to a certain file... but here i want 2 to be out on the same stream as 1, there is no output on one, so no redirection and i tell this after the command so that the redirection and the dup'ing of descriptors is not negated (which is the case in 2>&1 >"file" , the dup'ing is overriden in the last part as i tell that i want ONLY stdout to be sent to file)..

i hope i am right...
 
Old 10-08-2003, 06:08 PM   #8
Satriani
Member
 
Registered: Mar 2003
Location: The Netherlands
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418

Original Poster
Rep: Reputation: 30
Wow, this is confusing me.....

So, 2>1 means, send all stderr to stdout. Thats clear
Then i want stdout to a "file" so I think: 2>1>"file" correct?

whats the & sign doing there?
 
Old 10-08-2003, 09:16 PM   #9
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
Satriani, Close.

2>1 means redirect stderr to a file called 1 in the local directory

then 2>&1 means redirect all stderr (file descriptor 2) to stdout (file descriptor 1).. The & is required otherwise you would not be able to direct output to a file called 1 (not the file descriptor 1)...

So on the right of the '>' to reference a file descriptor (say stdout) it is &1 or for stderr &2 etc...

Cheers..
 
Old 10-09-2003, 03:43 AM   #10
Satriani
Member
 
Registered: Mar 2003
Location: The Netherlands
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418

Original Poster
Rep: Reputation: 30
Ahhhhhh thats making sense!
Thanks alot for your help. I'm beginning to loose my newbie status with all of your help!!! :-)

But, then in the example kev gave:
Quote:
> /dev/null 2>&1 & would work much better than 2>&1 > /dev/null &
it looks to me as:
all default output to /dev/null then all stderr to stdout. Correct?

Then the second option is: send all stderr to stdout, and send all stdout to /dev/null ?
(Am I still on the right track here?) Why is the first option better then the second?

thanks alot!
 
Old 10-09-2003, 06:47 AM   #11
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
I'm not too sure, and hopefully someone will explain in detail for us.

I just spent about 15 minutes trying to illistrate how it would work, but I can't for the life of me work out the semantic difference between the two statements. I know from experience that it does work but I can figure out the mechanism behind it. I typed about 500 words tying to explain what is happening but each time I attempted they both came out with basically the same result .

Please if you know the technical mechanics involved here can you please explain in detailed lay-mans terms.

How does " >/dev/null 2>&1" achieve what we want and " 2>&1 >/dev/null" does not?

Cheers...
 
Old 10-09-2003, 07:03 AM   #12
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
from the rute users guide
Now try

ls existing_file non-existing_file 1>A 2>&1
cat A

Now A contains both the error message and the normal output. The >& is called a redirection operator. x >&y tells the shell to write pipe x into pipe y. Redirection is specified from right to left on the command-line. Hence, the above command means to mix stderr into stdout and then to redirect stdout to the file A.

Finally,

ls existing_file non-existing_file 2>A 1>&2
cat A

We notice that this has the same effect, except that here we are doing the reverse: redirecting stdout into stderr and then redirecting stderr into a file A.

To see what happens if we redirect in reverse order, we can try,

ls existing_file non-existing_file 2>&1 1>A
cat A

which means to redirect stdout into a file A, and then to redirect stderr into stdout. This command will therefore not mix stderr and stdout because the redirection to A came first.
 
Old 10-09-2003, 07:22 AM   #13
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
Great work Kev82,

What I was missing was the right to left semantics! It now makes perfect sense.

This is such a good forum!

Cheers.
 
Old 10-17-2003, 01:31 PM   #14
Satriani
Member
 
Registered: Mar 2003
Location: The Netherlands
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418

Original Poster
Rep: Reputation: 30
Ahhhh, that is indeed a great answer! I now start to understand the difference !!!!

Thanks alot!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What would happen if I where to cat /dev/mem > /dev/null Joey.Dale Linux - General 11 07-26-2009 12:46 PM
/dev/null sachitha Programming 5 08-07-2005 11:25 AM
mv c:\WINDOWS /dev/null; mount /dev/hda treehead LinuxQuestions.org Member Intro 5 10-19-2004 08:53 AM
1> /dev/null 2> /dev/null elyk Programming 9 09-20-2004 05:44 PM
>/dev/null lackluster Linux - Networking 5 06-27-2002 09:54 PM


All times are GMT -5. The time now is 01:35 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration