A bash question

This is a discussion on A bash question within the Linux Programming forums, part of the Platform Specific Boards category; Hi, I am trying to redirect my output to a file and need to first put a command into a ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    A bash question

    Hi,
    I am trying to redirect my output to a file and need to first put a command into a string. Here is the error:
    Code:
    bash-3.2$ cc="(date && time ls -l -a && date) > ./tmp/log"
    bash-3.2$ $cc
    bash: (date: command not found
    What's wrong?
    Thanks and regards!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm not aware of a command called (date either.

    What are you trying to use the parentheses for, exactly?

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    The reason of using the parentheses is that I want to execute
    Code:
    date && time ls -l -a && date
    and make its output redirected to file ./tmp/log

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    old-school, you use back-ticks
    Code:
    cc=`date && time ls -l -a && date`
    new-school, I think it's something like
    Code:
    cc=$(date && time ls -l -a && date)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Back-ticks execute the command and store the result in the variable. So if you tried to then execute $cc you'd get things like "Tue: command not found".

    I can get parentheses to work when I type them straight on the command line, but not inside a variable like that. More research is needed.

  6. #6
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    $() is the same as ``. But better, since easier to read (and for me, to type) and can't mix up with ''.

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by lehe View Post
    Hi,
    I am trying to redirect my output to a file and need to first put a command into a string. Here is the error:
    Code:
    bash-3.2$ cc="(date && time ls -l -a && date) > ./tmp/log"
    bash-3.2$ $cc
    bash: (date: command not found
    What's wrong?
    Thanks and regards!
    What you did is okay, but I think when you want to "run" a variable, you have to eval it:
    Code:
    eval $cc
    . . . but I cannot recall at the moment. It is worth a try.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lehe View Post
    Hi,
    I am trying to redirect my output to a file and need to first put a command into a string.
    You will have to redirect each command before &&. However, you cannot effectively run a string with && in it anyway.

    One option is to put this into an array, which maybe is what you were trying to do with the brackets before, since that is what they'll do:
    Code:
    cc=('date' 'bash -c "time ls -l"' 'date')
    The reason I used bash -c "time ls -l" is because time is a bash builtin, and if you try to run it from a variable this way you will get
    bash: time: No such file or directory
    Now here's a weird thing about bash. If you do this:
    Code:
    for e in ${cc[@]}; do echo $e; done
    You will get this:
    date
    bash
    -c
    "time
    ls
    -l"
    date

    It seems an element here is not really what one might hope. However, this works:
    Code:
    for e in 0 1 2; do echo ${cc[$e]}; done
    date
    bash -c "time ls -l"
    date

    So, it looks like all we need now is:
    Code:
    for e in 0 1 2; do ${cc[$e]}>>tmp.txt; done
    Which will work (...almost) and amounts to (almost) same thing as using &&. I say almost because in fact you get a strange parsing related error, again from the bash/time/ls construct:
    ls: -c: line 0: unexpected EOF while looking for matching `"'
    ls: -c: line 1: syntax error: unexpected end of file

    Since the next "e" does execute dispite the failure of ${cc[1]}, this is not really equivalent to an && -- for that you need to get more complicated.

    But I cannot get 'bash -c "time ls"' to run this way. Non builtins with parameters are fine.

    I'll go ask some other bunch of monkeys tho and see if they have anything wise to add.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    ((stuff))
    Or you can use eval. (Thanks Kennedy for getting my one new thing for today out of the way early so I could relax!)

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    Or you can use eval.
    Didn't know bash had an eval. That simplifies that.

    One last thing: to get the output of "time" into the file:
    Code:
    cc="date && time ls -d && date"
    eval $cc >tmp.txt 2>tmp.txt
    since it appears on stderr (aka channel 2).
    Last edited by MK27; 07-29-2009 at 02:42 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21