Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Terminal/command line skills needed

  1. #1
    Join Date
    Jul 2008
    Location
    Luleå, Sweden
    Beans
    88
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Question Terminal/command line skills needed

    Hi! I wasn't sure about where to ask this so apologies if it's in the wrong section.

    I'm running CrunchBang wich is based on Debian and my screensaver is XScreenSaver. When playing a movie in Gnome Mplayer the screensaver activates unless I edit ~/.mplayer/config and enter (according to XScreenSaver faq)
    Code:
    heartbeat-cmd="xscreensaver-command -deactivate >&- 2>&- &"
    My question is, what does the part in bold mean?
    "You must learn from the mistakes of others. You can't possibly live long enough to make them all yourself."
    -- Sam Levenson
    Fujitsu Siemens Lifebook E8020D | Triple booting CrunchBang, Ubuntu and Windows 7

  2. #2
    Join Date
    Dec 2011
    Beans
    10

    Re: Terminal/command line skills needed

    I think the ampersand (the & symbol) puts the task in the background. I'm not sure about this context though. So if you wrote
    Code:
    mv largefile.txt largefile2.txt&
    Then this would put it into the background of the terminal so the command prompt would show up right away, and to list background terminal tasks, you can use
    Code:
    ps
    for processes or
    Code:
    jobs
    Anyone else?

  3. #3
    Join Date
    Dec 2011
    Beans
    132

    Re: Terminal/command line skills needed

    http://tldp.org/LDP/abs/html/io-redirection.html

    Looks like you're telling it to close both stdout and stderr with the first two. Wonder if that's the same thing as pointing it to /dev/null?

  4. #4
    Join Date
    Dec 2011
    Beans
    10

    Re: Terminal/command line skills needed

    Oh and I forgot to mention, I think the 2> means that it goes to the error output. Linux has several outputs.
    > standard output
    1> standard output
    2> error output

    For example if your typed
    Code:
    ls > files.txt
    Then this command would list the current directory and put it into a text file called files. If you typed
    Code:
    ls /fakedirectory 2> files.txt
    Then this would put
    Code:
    ls: cannot access /fakedirectory: No such file or directory
    into a file called files.txt. This would not happen with a normal > option because error output in not mixed with standard output. Hope this helps.

  5. #5
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,263
    Distro
    Ubuntu

    Re: Terminal/command line skills needed

    The ampersand on its own means to put it into the background as an asynchronous background task. Thus, that's the final ampersand that does it.

    The symbol ">" means to redirect output. There are two usual outputs, one being stdout or 1, which is normal output, and the other stderr or 2, which is error messages.

    Thus: "1>&-" means to send stderr to something called "&-" (more on that in a moment) instead of its usual terminal; and "2>&-" means to also send stderr to "&-".

    ">&-" is shorthand for "1>&-".

    Incidentally, "&>" is shorthand for "both 1 and 2 >", thus ">&-1 2>&-" could be shortened to "&>&-".

    Confused yet?

    I do not quite understand what "&-" means, but I think it means to send output to the owning variable; in this case we don't know what it is, because it gets passed to XScreenSaver.

    You can read more about redirection by using the command:
    Code:
    man bash
    and searching for REDIRECTION. You can also find more about background jobs in the same manual. There is a copy of the bash manual on-line if you find that easier.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  6. #6
    Join Date
    Jul 2007
    Location
    Magic City of the Plains
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: Terminal/command line skills needed

    Moved to Other OS/Distro Talk.

  7. #7
    Join Date
    Jul 2008
    Location
    Luleå, Sweden
    Beans
    88
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Terminal/command line skills needed

    Ok, thanks to all three of you.

    This is output from running the command in the terminal:
    Code:
    isen@crunchbang:~$ xscreensaver-command -deactivate >&- 2>&- &
    [1] 9007
    isen@crunchbang:~$ xscreensaver-command -deactivate >&- 2>&- &
    [2] 9008
    [1]   Done                    xscreensaver-command -deactivate 1>&- 2>&-
    isen@crunchbang:~$
    This is what it looks like without redirecting, just putting it in the background.
    Code:
    isen@crunchbang:~$ xscreensaver-command -deactivate &
    [1] 9018
    isen@crunchbang:~$ xscreensaver-command: not active: idle timer reset.
    It looked like I could continue using the terminal but then the idle timer reset message interrupted and stole the prompt. Anyway, I'm marking this thread as solved
    "You must learn from the mistakes of others. You can't possibly live long enough to make them all yourself."
    -- Sam Levenson
    Fujitsu Siemens Lifebook E8020D | Triple booting CrunchBang, Ubuntu and Windows 7

  8. #8
    Join Date
    Dec 2011
    Beans
    10

    Re: Terminal/command line skills needed

    A word of advice, go out and get a good book on the terminal, it's a lifesaver! The terminal will be 90 percent the same across most distros unlike KDE, Gnome, and Unity with their constant changes. A good book I'd recommend is "A Practical Guide to Linux Commands, Editors, And Shell Programming" by Mark Sobell.

  9. #9
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Terminal/command line skills needed

    [n]<&word is used to duplicate input file descriptors and [n]>&word is used to duplicate output file descriptors. If word evaluates to -, then file descriptor n is closed.

    >&- or 1>&- closes file descriptor 1 (stdout)
    and
    2>&- closes file descriptor 2 (stderr)

    See:
    Code:
    man bash | less +/" +Duplicating File Descriptors"
    man bash | less +/"^REDIRECTION"
    http://mywiki.wooledge.org/BashGuide/InputAndOutput
    http://wiki.bash-hackers.org/syntax/redirection
    and the POSIX specification:
    http://pubs.opengroup.org/onlinepubs...html#tag_18_07

    Another option would be to redirect stdout & stderr to /dev/null.
    BASH:
    Code:
    command &> /dev/null
    Note: this is not standard, so I'd avoid it in scripts. But it's shorter than the POSIX form, hence it's handy in an interactive BASH shell.

    The POSIX form is:
    Code:
    command > /dev/null 2>&1

  10. #10
    Join Date
    Jul 2008
    Location
    Luleå, Sweden
    Beans
    88
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Terminal/command line skills needed

    Quote Originally Posted by kalfusisagod View Post
    A word of advice, go out and get a good book on the terminal, it's a lifesaver! The terminal will be 90 percent the same across most distros unlike KDE, Gnome, and Unity with their constant changes. A good book I'd recommend is "A Practical Guide to Linux Commands, Editors, And Shell Programming" by Mark Sobell.
    Yeah, that's on my "todo list". I've got one in a Swedish translation/rework from Gareth Anderson's GNU/Linux Command-Line Tools Summary but I'd like to have another one which digs a little deeper. I will have a look at the one you mentioned.
    "You must learn from the mistakes of others. You can't possibly live long enough to make them all yourself."
    -- Sam Levenson
    Fujitsu Siemens Lifebook E8020D | Triple booting CrunchBang, Ubuntu and Windows 7

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •