Differences between revisions 1 and 11 (spanning 10 versions)
Revision 1 as of 2005-08-07 18:51:18
Size: 1257
Editor: GreyCat
Comment: copied from heiner's wiki, converted to moin
Revision 11 as of 2008-11-22 14:08:32
Size: 2665
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
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? Answer: duplicating fds is not sufficient, because it duplicates the file descriptor number, not the data. But you can still use {{{tee}}}.
 * Somewhere the content of variables in my script lose whitespace. Answer: _echo "$var" - quoting_
 * 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
 * How can I handle command line arguments in a shell script? Answer: {{{getopts(1)}}}
 * How can I have variable variables, e.g. myvar=prefix$othervar? Answer: {{{eval(1)}}}
If you want to help with the BashFAQ, you could try to answer one of the following questions. Just answer it, copy it to a new subpage of the BashFAQ page (e.g. [[BashFAQ/101]]), and remove the question here.
 * 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)}}}
 * Somewhere the content of variables in my script lose whitespace.
  *
echo "$var" - quoting. See BashPitfalls for more.
 * 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#faq45|Bash FAQ #45]] now.
  * It's also covered in ProcessManagement.
 
* How can I handle command line arguments in a shell script?
  * {{{getopts}}}, or a while/shift loop (see [[BashF
AQ#faq35|Bash FAQ #35]])
 * How can I have variable variables, e.g. myvar=prefix$othervar?
  *
Answer: {{{eval}}} -- see also [[BashFAQ#faq6|Bash FAQ #6]] and the warnings in [[BashFAQ#faq48|#48]].
  * If {{{othervar}}} is an integer, use an [[BashFAQ#faq5|array]] instead.
Line 9: Line 18:
 * How to determine Yesterday's date? Answer: e.g. GNU date -d "1970-01-01 $seconds seconds"
 * How to convert Unix time stamps to a date (and vice versa)? Answer: GNU date, awk+strftime()
 * How to redirect stderr to a pipeline?
  * This can't be done in any straightforward manner because there's no hook in Bash to execute arbitrary code after a user presses Enter but before a command is executed. The DEBUG trap is triggered after each command, and the PS1 variable is triggered when a prompt is displayed, which is also after the command, rather than before.
 * How to determine Yesterday's date?
  * date -d'yesterday' # GNU date
  * date -r $(( $(date +%s) - 86400 )) # OpenBSD date
 * How to convert Unix time stamps to a date (and vice versa)?
  * [[BashFAQ#faq70|FAQ #70]]
 * How to redirect stderr to a pipeline? Now [[BashFAQ#faq47|Bash FAQ #47]]
  * command 2>&1 | command
  * To discard stdout entirely: command 2>&1 >/dev/null | command
Line 13: Line 28:
  * 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?
  * i think this does the job, have not had time to test: http://tldp.org/LDP/abs/html/process-sub.html
   Commands in a process substitution ( >(cmd) or <(cmd) ) are definitely run in a subshell. I'm not sure I understand what this question is really asking. Maybe it's related to [[BashFAQ#faq84|FAQ 84]].

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 a new subpage of the BashFAQ page (e.g. BashFAQ/101), 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?
  • How can I handle command line arguments in a shell script?
  • How can I have variable variables, e.g. myvar=prefix$othervar?
    • Answer: eval -- see also Bash FAQ #6 and the warnings in #48.

    • 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?
    • This can't be done in any straightforward manner because there's no hook in Bash to execute arbitrary code after a user presses Enter but before a command is executed. The DEBUG trap is triggered after each command, and the PS1 variable is triggered when a prompt is displayed, which is also after the command, rather than before.
  • How to determine Yesterday's date?
    • date -d'yesterday' # GNU date
    • date -r $(( $(date +%s) - 86400 )) # OpenBSD date
  • How to convert Unix time stamps to a date (and vice versa)?
  • How to redirect stderr to a pipeline? Now Bash FAQ #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?
    • i think this does the job, have not had time to test: http://tldp.org/LDP/abs/html/process-sub.html

      • Commands in a process substitution ( >(cmd) or <(cmd) ) are definitely run in a subshell. I'm not sure I understand what this question is really asking. Maybe it's related to FAQ 84.

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