PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Bash script output redirection question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Bash script output redirection question

Réponse
 
LinkBack Outils de la discussion
Vieux 01/12/2006, 22h58   #1
kk_oop@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Bash script output redirection question

Hi. I am currently redirecting output from a bash script by doing this
in the script:

{
....bash commands..
} > "$filename" 2>&1

I now would like to add the option to also display onto the standard
output. So I I'd do something like this:

{
....bash commands..
} 2>&1 | tee "$filename"

The problem I am having, though, is that I'd like to have a script
option that will let the user choose one or the other. I tried having
a variable like this:

redirection_descriptor="> ${filename} 2>&1"

or

redirection_descriptor="2>&1 | tee ${filename}"

based on the user's option. Then I tried to do this:

{
....bash commands..
} $redirection_descriptor

but that gave me a syntax error on $redirection_descriptor.

Any suggestions how to do this?

Thanks!

Ken

  Réponse avec citation
Vieux 01/12/2006, 23h26   #2
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bash script output redirection question

2006-12-1, 14:58(-08), kk_oop@yahoo.com:
> Hi. I am currently redirecting output from a bash script by doing this
> in the script:
>
> {
> ...bash commands..
> } > "$filename" 2>&1
>
> I now would like to add the option to also display onto the standard
> output. So I I'd do something like this:
>
> {
> ...bash commands..
> } 2>&1 | tee "$filename"
>
> The problem I am having, though, is that I'd like to have a script
> option that will let the user choose one or the other. I tried having
> a variable like this:

[...]

Easiest is probably to do something like:

main () {
...bash commands...
}

if [ "$opt_t" = true ]; then
main 2>&1 | tee -- "$filename"
else
main > "$filename" 2>&1
fi

You could also do:

if [ "$opt_t" = true ]; then
logging() {
tee -- "$filename"
}
else
logging() {
cat > "$filename"
}
fi

{
...bash commands...
} 2>&1 | logging

But that means your bash commands standard output will be a pipe
even when that could have been avoided.

--
Stéphane
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h08.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,08769 seconds with 10 queries