#1 Le 16/11/2016, à 11:13
- maladra
Comment vérifier qu'un paramètre est un .tar ?
Bonjour,
J'ai un problème en shell, je dois réaliser un script et vérifier que le premier paramètre est un .tar mais je ne vois pas comment faire.
Un peu d'aide serait la bienvenue
Merci d'avance
Hors ligne
#2 Le 16/11/2016, à 11:43
- pingouinux
Re : Comment vérifier qu'un paramètre est un .tar ?
Bonjour,
[[ $1 =~ .*\.tar ]] && echo oui || echo non
Hors ligne
#3 Le 16/11/2016, à 11:49
- maladra
Re : Comment vérifier qu'un paramètre est un .tar ?
a quoi sert les double crochet et le ~ ?
et comment puis-je parcourir les fichiers de mon .tar pour y trouver les fichiers passés en paramètre ? :
ex : sh script.sh archive.tar fich1.txt fich2.txt [...]
--> fich1.txt
fich1.txt
fich2.txt
Dernière modification par maladra (Le 16/11/2016, à 11:53)
Hors ligne
#4 Le 16/11/2016, à 12:23
- pingouinux
Re : Comment vérifier qu'un paramètre est un .tar ?
Je t'invite à regarder la documentation. Voici deux extraits de man bash
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the conditional expression
expression. Expressions are composed of the primaries described below under CONDI‐
TIONAL EXPRESSIONS. Word splitting and pathname expansion are not performed on the
words between the [[ and ]]; tilde expansion, parameter and variable expansion,
arithmetic expansion, command substitution, process substitution, and quote removal
are performed. Conditional operators such as -f must be unquoted to be recognized
as primaries.When used with [[, the < and > operators sort lexicographically using the current
locale.See the description of the test builtin command (section SHELL BUILTIN COMMANDS below) for
the handling of parameters (i.e. missing parameters).When the == and != operators are used, the string to the right of the operator is consid‐
ered a pattern and matched according to the rules described below under Pattern Matching,
as if the extglob shell option were enabled. The = operator is equivalent to ==. If the
shell option nocasematch is enabled, the match is performed without regard to the case of
alphabetic characters. The return value is 0 if the string matches (==) or does not match
(!=) the pattern, and 1 otherwise. Any part of the pattern may be quoted to force the
quoted portion to be matched as a string.An additional binary operator, =~, is available, with the same precedence as == and !=.
When it is used, the string to the right of the operator is considered an extended regular
expression and matched accordingly (as in regex(3)). The return value is 0 if the string
matches the pattern, and 1 otherwise.
* Expands to the positional parameters, starting from one. When the expansion is not
within double quotes, each positional parameter expands to a separate word. In
contexts where it is performed, those words are subject to further word splitting
and pathname expansion. When the expansion occurs within double quotes, it expands
to a single word with the value of each parameter separated by the first character
of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c
is the first character of the value of the IFS variable. If IFS is unset, the
parameters are separated by spaces. If IFS is null, the parameters are joined
without intervening separators.
@ Expands to the positional parameters, starting from one. When the expansion occurs
within double quotes, each parameter expands to a separate word. That is, "$@" is
equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word,
the expansion of the first parameter is joined with the beginning part of the orig‐
inal word, and the expansion of the last parameter is joined with the last part of
the original word. When there are no positional parameters, "$@" and $@ expand to
nothing (i.e., they are removed).
Que veux-tu faire lorsque tu as vérifié que le premier paramètre est un fichier .tar ? Extraire ceux que tu indiques dans les paramètres suivants ?
Hors ligne
#5 Le 16/11/2016, à 12:28
- maladra
Re : Comment vérifier qu'un paramètre est un .tar ?
non, pas les extraires, juste dire si les fichiers sont présent dans l'archive
Hors ligne
#6 Le 16/11/2016, à 15:10
- pingouinux
Re : Comment vérifier qu'un paramètre est un .tar ?
Tu peux faire un truc de ce genre, à adapter en fonction de la façon dont l'archive a été créée.
#!/bin/bash
if [[ $1 =~ .*\.tar && -f $1 ]]
then
fichier_tar=$1
shift
for fic in "$@"
do
if ! tar tf "$fichier_tar" "$fic" 2>/dev/null
then
echo "$fic n'est pas dedans"
fi
done
else
echo "$1 n'existe pas ou n'est pas un .tar"
fi
Ajouté : Au besoin, tu montres le retour de
tar tf ton_fichier.tar | head -5
Dernière modification par pingouinux (Le 16/11/2016, à 17:06)
Hors ligne
#7 Le 16/11/2016, à 17:12
- Watael
Re : Comment vérifier qu'un paramètre est un .tar ?
salut,
if [[ $1 =~ .*\.tar ]] && [[ -f $1 ]]
c'est drôle, j'aurais fait le contraire (je ne suis pas certain que ce soit important) :
if test -f "$1" && test "${1##*.}" = "tar"
et un peu différemment.
Connected \o/
Welcome to sHell. · eval is evil.
Hors ligne
#8 Le 20/11/2016, à 23:35
- alius
Re : Comment vérifier qu'un paramètre est un .tar ?
salut, je pense que le mieux est encore de vérifier la valeur de $? après un appel à tar -t fichier.tar.
Si $? vaut 0 c'est une archive tar valide sinon non.
Alius
Hors ligne