Differences between revisions 6 and 8 (spanning 2 versions)
Revision 6 as of 2005-11-18 12:52:45
Size: 1520
Editor: 19
Comment: example tee usage
Revision 8 as of 2006-05-23 16:50:00
Size: 1838
Editor: GreyCat
Comment: fix up formatting, expand a few of the cryptic ones
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
 * How can I redirect the output of the script to both standard output and a log file? Answer: duplicating fds is not sufficient, because it duplicates the file descriptor number, not the data. But you can still use {{{tee}}}: {{{exec > >(tee log)}}}  * How can I redirect the output of the script to both standard output and a log file?
  * D
uplicating fds is not sufficient, because it duplicates the file descriptor number, not the data.
  *
But you can still use {{{tee}}}: {{{exec > >(tee log)}}}
Line 9: Line 11:
 * How can I handle command line arguments in a shell script? Answer: {{{getopts(1)}}}  * How can I handle command line arguments in a shell script?
 * {{{getopts}}}, or a while/shift loop (see BashFaq #35)
Line 14: Line 17:
 * How to determine Yesterday's date? Answer: date -d'yesterday'
 * How to convert Unix time stamps to a date (and vice versa)? Answer: GNU date, awk+strftime()
 * How to redirect stderr to a pipeline? (done: BashFaq #47
 * How to determine Yesterday's date?
  * date -d'yesterday' # requires GNU date
 * How to convert Unix time stamps to a date (and vice versa)?
  * gdate -d "1970-01-01 UTC + $time seconds"
  * something involving awk and strftime()
  * perl -e "print scalar localtime $time, \"\n\""
 * How to redirect stderr to a pipeline? Now BashFaq #47
Line 19: Line 26:
 * How can I redirect file names using sequential numbers to avoid overwriting existing ones? Answer: mv --backup=numbered  * How can I redirect file names using sequential numbers to avoid overwriting existing ones?
  *
mv --backup=numbered  # requires GNU mv
 * How can I set the output of a command to a variable without executing a subshell (i.e. $() ) or writing to a file and reading it back?

Bash Open Questions

If you want to help with the BashFaq, you could try to answer one of the following questions. Just answer it, copy it to the BashFaq page, and remove the question here.

  • How can I redirect the output of the script to both standard output and a log file?
    • Duplicating fds is not sufficient, because it duplicates the file descriptor number, not the data.
    • But you can still use tee: exec > >(tee log)

  • Somewhere the content of variables in my script lose whitespace.
  • How can I ensure that only once instance of my script is running at a time?
    • Answer: e.g. lockfile. This should be answered in the UnixFaq

    • It's also BashFaq #45 now.

  • How can I handle command line arguments in a shell script?
    • getopts, or a while/shift loop (see BashFaq #35)

  • How can I have variable variables, e.g. myvar=prefix$othervar?
    • Answer: eval

    • If othervar is an integer, use an array instead.

  • How can I make bash set the xterm title to the command it is currently executing?
  • How to determine Yesterday's date?
    • date -d'yesterday' # requires GNU date
  • How to convert Unix time stamps to a date (and vice versa)?
    • gdate -d "1970-01-01 UTC + $time seconds"
    • something involving awk and strftime()
    • perl -e "print scalar localtime $time, \"\n\""
  • How to redirect stderr to a pipeline? Now BashFaq #47

    • command 2>&1 | command

    • To discard stdout entirely: command 2>&1 >/dev/null | command

  • How can I redirect file names using sequential numbers to avoid overwriting existing ones?
    • mv --backup=numbered # requires GNU mv
  • How can I set the output of a command to a variable without executing a subshell (i.e. $() ) or writing to a file and reading it back?

BashOpenQuestions (last edited 2023-06-23 18:42:54 by larryv)