Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 23/05/2013, à 03:06

fnux

Big problème de variables avec BASH et/ou SH.

Bonjour tout le monde,

Bon, maintenant je rencontre un nouveau problème du nombre de variables utilisées dans un script.

J'explique mon cas :

En fait, j'ai déjà écrit un script assez long (environ 1200 lignes) qui fonctionne très bien et qui installe différents outils dont des langages de programmation comme c++, go, lua, mono, scala, etc. et différents autres outils pour un usage bien particulier.

Ce script comporte pas moins de 260 messages divers (informations, questions, choix proposés, avertissements, etc.)

Tous ces dialogues sont affichés soit par la commande "echo", soit par la commande "read".

Et j'ai écrit les messages du script de base en Anglais.

Mais maintenant, j'aimerai rendre ce script multilingue (dans un premier temps, ajout du Français, puis ensuite de l'Allemand, de l'Espagnol, de l'Italien et du Néerlandais) et que la langue utilisée soit automatiquement déterminée et appliquée en fonction de la langue installée.

Donc je m'y prends de la manière suivante par exemple pour le français et l'anglais (rassurez-vous, je ne vais pas mettre les 1200 lignes pour expliquer le problème rencontré). wink

Je remplace chaque message affiché par "echo" ou "read" par une variable qui contient le texte à afficher comme dans l'exemple ci-dessous :

(PS: Dans mon code, je mets aussi en commentaire les messages d'origine pour pouvoir m'y retrouver par la suite en cas de problème). wink

#!/bin/sh
# ABCD_install.sh
#

#
# Function that displays an intorduction of this script.
#
dialog_intro() {
  echo ""
  PS1=$BLUE
  echo $dlg023 #"${PS1}Hello \033[01;32m$ME"
  echo "${PS1}"
  echo $dlg022 #"This script installs \033[01;31mABCD\033[01;34m as well as all the necessary tools & dependencies"
  echo $dlg021 #"needed to play with the already 15 supported programming languages accordingly"
  echo $dlg020 #"to your OS specifications."
  echo ""
  echo $dlg019 #"Your OS is: \033[1;31m$SYSTEM $VENDOR $RELEASE $LTS \033[1;34mwith the following characteristics:\n"
  echo $dlg017 #"\033[1;34m Name:    \033[1;31m$NAME\033[34m\n Flavor:  \033[1;31m$FLAVOR bits\033[1;34m\n Version: \033[1;31m$VERSION\033[34m\n Kernel:  \033[1;31m$KERNEL\033[34m\n\n The ABCD install folder will be: \033[1;31m$HOME$ABCDFOLDER\n\033[1;34m The EFGH install folder will be:  \033[1;31m$HOME$EFGHFOLDER\n\033[1;34m The TEMP install folder will be:  \033[1;31m$HOME$TMPINSTALL"
  if [ "$NAME" = "maverick" ]
    then
      echo ""
      PS1=$RED
      echo $dlg016 #"${PS1}Oops! \033[01;34mWe're sorry but \033[01;31m"$VENDOR $RELEASE"\033[01;34m can't be updated anymore!"
      PS1=$BLUE
      echo "$PS1"
      echo $dlg015 #"However, you can use \033[01;31mABCD\033[01;34m on \033[01;31m"$VENDOR $RELEASE"\033[01;34m but you won't"
      echo $dlg014 #"be able to use C++, C#, Java, Javascript server side, Lua, Objective C/C++, PHP,"
      echo $dlg013 #"Ruby or Scala to create servlets."
      echo ""
      echo $dlg012 #"Meanwhile, you still will be able to use ANSI C, ASM, GO, Perl and Python."
	elif [ "$NAME" = "natty" ]
		then
      echo ""
      PS1=$RED
      echo $dlg011 #"${PS1}Oops! \033[01;34mWe're sorry but \033[01;31m"$VENDOR $RELEASE"\033[01;34m can't be fully updated anymore!"
      PS1=$BLUE
      echo "$PS1"
      echo $dlg010 #"So, \033[01;31mABCD\033[01;34m can't use Scala with \033[01;31m"$VENDOR $RELEASE"\033[01;34m!"
      echo ""
      echo $dlg009 #"However, you can use \033[01;31mABCD\033[01;34m on \033[01;31m"$VENDOR $RELEASE"\033[01;34m with ANSI C, ASM, C++, C#, Go,"
      echo $dlg008 #"Java, Javascript server side, Lua, Objective C/C++, Perl, PHP, Python and Ruby"
      echo $dlg007 #"to create servlets."
  fi
}
# --------------------------------------------------------------------------------

#
# function that stops the script if the script isn't started with root privileges
#
ask_for_root() {
  if [ "$USER" != "root" ]
    then
      PS1="\033[01;31m"
      echo "${PS1}"
      echo $dlg006 #"WARNING"
      echo $dlg005 #"-------"
      echo ""
      echo $dlg004 #"This script can't be launched without the root privileges (i.e. sudo or su)!"
      PS1="\033[01;32m"
      echo "${PS1}"
      echo $dlg003 #"Please restart the script with the command: \033[01;37msudo ./ABCD_install.sh\033[01;32m"
      echo $dlg002 #"or use the \033[01;37msu\033[01;32m account and start: \033[01;37ms./ABCD_install.sh"
      echo ""
      PS1="\033[01;33m"
      echo "${PS1}$dlg001" #"Thank you."
      PS1="\033[0m"
      echo "${PS1}"
      exit
  fi
}
# --------------------------------------------------------------------------------

#
# set the messages in french
#
set_language_fr() {
  dlg001="Merci."
  dlg002="ou d'utiliser le compte \033[01;37msu\033[01;32m et de lancer: \033[01;37m./ABCD_install.sh"
  dlg003="Merci de relancer le script par la commande: \033[01;37msudo ./ABCD_install.sh\033[01;32m"
  dlg004="Ce script ne peut pas se lancer sans les droits du compte root (sudo or su)!"
  dlg005="---------"
  dlg006="Attention"
  dlg007="pour creer des servlets."
  dlg008="Java, Javascript cote serveur, Lua, Objective C/C++, Perl, PHP, Python et Ruby"
  dlg009="Mais vous pouvez utiliser \033[01;31mABCD\033[01;34m on \033[01;31m"$VENDOR $RELEASE"\033[01;34m avec ANSI C, ASM, C++, C#, Go,"
  dlg010="Donc, \033[01;31mABCD\033[01;34m ne peut pas utiliser Scala avec \033[01;31m"$VENDOR $RELEASE"\033[01;34m!"
  dlg011="${PS1}Oops! \033[01;34mDesole mais \033[01;31m"$VENDOR $RELEASE"\033[01;34m ne peut plus etre totalement mis a jour!"
  dlg012="Mais vous pourrez toujours utiliser ANSI C, ASM, GO, Perl et Python."
  dlg013="Ruby ou Scala pour creer des servlets."
  dlg014="pouvoir utiliser C++, C#, Java, Javascript server side, Lua, Objective C/C++, PHP,"
  dlg015="Mais vous pouvez utiliser \033[01;31mABCD\033[01;34m avec \033[01;31m"$VENDOR $RELEASE"\033[01;34m sans"
  dlg016="${PS1}Oops! \033[01;34mDesole mais \033[01;31m"$VENDOR $RELEASE"\033[01;34m ne peut plus etre mis a jour!"
  dlg017="\033[1;34m Nom:     \033[1;31m$NAME\033[34m\n Flavor:  \033[1;31m$FLAVOR bits\033[1;34m\n Version: \033[1;31m$VERSION\033[34m\n Noyau:   \033[1;31m$KERNEL\033[34m\n\n Dossier d'installation de ABCD : \033[1;31m$HOME$ABCDFOLDER\n\033[1;34m Dossier d'installation de EFGH :  \033[1;31m$HOME$EFGHFOLDER\n\033[1;34m Dossier d'installation temporaire:\033[1;31m$HOME$TMPINSTALL"
  dlg019="Votre OS est : \033[1;31m$SYSTEM $VENDOR $RELEASE $LTS \033[1;34mavec les caracteristiques suivantes :\n"
  dlg020="specifications de votre OS."
  dlg021="pour utiliser les 15 langages de programmation deja supportes en accord avec les"
  dlg022="Ce script installe \033[01;31mABCD\033[01;34m et tous les outils et dependences necessaires"
  dlg023="${PS1}Bonjour \033[01;32m$ME"
  dlg024="Donc le dossier de ABCD restera dans : \033[1;31m$ABCDFOLDER\n\033[1;34met le dossier de EFGH restera dans    : \033[1;31m$EFGHFOLDER\n\033[1;34met le dossier temporaire restera dans : \033[1;31m$TMPINSTALL"
  dlg025="Donc le dossier de ABCD restera dans : \033[1;31m$ABCDFOLDER\n\033[1;34met le dossier de EFGH restera dans    : \033[1;31m$EFGHFOLDER\n\033[1;34met le dossier temporaire restera dans : \033[1;31m$TMPINSTALL"
  dlg026="Le nouveau dossier pour ABCD sera    : \033[1;31m$ABCDFOLDER\n\033[1;34met le nouveau dossier pour EFGH sera  : \033[1;31m$EFGHFOLDER\n\033[1;34met le nouveau dossier temporaire sera : \033[1;31m$TMPINSTALL"
  dlg027="Voulez-vous changer le dossier d'installation : O/n (retour vide = Non)? :"
  dlg028="La premiere etape est de definir ou le script installera ABCD."
  dlg029="${PS1}--------------------------------------------------------------------------------"
  dlg030="Le dossier d'installation n'a pas ete change!"
  dlg031="Le dossier d'installation n'a pas ete change!"
  dlg032="Confirmez-vous_le_nouveau_dossier_d'installation_:_O/n_(retour vide_=_Non)?_:_"
  dlg033="Le nouveau dossier d'installation sera : \033[1;31m$NEWHOME$ABCDFOLDER"
  dlg034="de ce script :\n\033[0;29m"
  dlg035="(exemple : \033[1;31m/home/user_name\033[1;34m) ou \033[1;31muser_name\033[1;34m est le \033[1;31mnom\03[1;34m de l'utilisateur"
  dlg036="${PS1}Entrez le \033[1;31mchemin\033[1;34m pour creer un nouveau \033[1;31mdossier\033[1;34m pour l'installation de ABCD"
  dlg037="Le dossier d'installation n'a pas ete change !"
  dlg038="${PS1}Bon! Le dossier d'installation sera : \033[1;31m$HOME"
  dlg039="Voulez-vous continuer: O/n (retour vide = Non) ? "
  dlg040="Vous avez arrete le script. Relancez le avec un bon nom de dossier d'installation !"
  dlg041="Au revoir."
}
# --------------------------------------------------------------------------------

#
# set the messages in english
#
set_language_us() {
  dlg001="Thank you."
  dlg002="or use the \033[01;37msu\033[01;32m account and start: \033[01;37m./ABCD_install.sh"
  dlg003="Please restart the script with the command: \033[01;37msudo sh ABCD_install.sh\033[01;32m"
  dlg004="This script can't be launched without the root privileges (i.e. sudo or su)!"
  dlg005="-------"
  dlg006="WARNING"
  dlg007="to create servlets."
  dlg008="Java, Javascript server side, Lua, Objective C/C++, Perl, PHP, Python and Ruby"
  dlg009="However, you can use \033[01;31mABCD\033[01;34m on \033[01;31m"$VENDOR $RELEASE"\033[01;34m with ANSI C, ASM, C++, C#, Go,"
  dlg010="So, \033[01;31mABCD\033[01;34m can't use Scala with \033[01;31m"$VENDOR $RELEASE"\033[01;34m!"
  dlg011="${PS1}Oops! \033[01;34mWe're sorry but \033[01;31m"$VENDOR $RELEASE"\033[01;34m can't be fully updated anymore!"
  dlg012="Meanwhile, you still will be able to use ANSI C, ASM, GO, Perl and Python."
  dlg013="Ruby or Scala to create servlets."
  dlg014="be able to use C++, C#, Java, Javascript server side, Lua, Objective C/C++, PHP,"
  dlg015="However, you can use \033[01;31mABCD\033[01;34m on \033[01;31m"$VENDOR $RELEASE"\033[01;34m but you won't"
  dlg016="${PS1}Oops! \033[01;34mWe're sorry but \033[01;31m"$VENDOR $RELEASE"\033[01;34m can't be updated anymore!"
  dlg017="\033[1;34m Name:    \033[1;31m$NAME\033[34m\n Flavor:  \033[1;31m$FLAVOR bits\033[1;34m\n Version: \033[1;31m$VERSION\033[34m\n Kernel:  \033[1;31m$KERNEL\033[34m\n\n The ABCD install folder will be: \033[1;31m$HOME$ABCDFOLDER\n\033[1;34m The EFGH install folder will be:  \033[1;31m$HOME$EFGHFOLDER\n\033[1;34m The TEMP install folder will be:  \033[1;31m$HOME$TMPINSTALL"
  dlg019="Your OS is: \033[1;31m$SYSTEM $VENDOR $RELEASE $LTS \033[1;34mwith the following characteristics:\n"
  dlg020="to your OS specifications."
  dlg021="needed to play with the already 15 supported programming languages accordingly"
  dlg022="This script installs \033[01;31mABCD\033[01;34m as well as all the necessary tools & dependencies"
  dlg023="${PS1}Hello \033[01;32m$ME"
  dlg024="So the ABCD install folder will stay as: \033[1;31m$ABCDFOLDER\n\033[1;34mand the EFGH install folder will stay as: \033[1;31m$EFGHFOLDER\n\033[1;34mand the TEMP install folder will stay as: \033[1;31m$TMPINSTALL"
  dlg025="So the ABCD install folder will stay as: \033[1;31m$ABCDFOLDER\n\033[1;34mand the EFGH install folder will stay as: \033[1;31m$EFGHFOLDER\n\033[1;34mand the TEMP install folder will stay as: \033[1;31m$TMPINSTALL"
  dlg026="So the new install folder will be in:     \033[1;31m$ABCDFOLDER\n\033[1;34mand the new EFGH install folder will be:  \033[1;31m$EFGHFOLDER\n\033[1;34mand the new TEMP install folder will be:  \033[1;31m$TMPINSTALL"
  dlg027="Do you want to change the install folder: Y/n (empty return = No)? :"
  dlg028="The first step is to define the folder where the script will install ABCD."
  dlg029="${PS1}--------------------------------------------------------------------------------"
  dlg030="The installation folder wasn't changed!"
  dlg031="The installation folder wasn't changed!"
  dlg032="Please_confirm_changing_the_install_folder:_Y/n_(empty_return_=_No)?:_"
  dlg033="The new installation folder will be: \033[1;31m$NEWHOME$ABCDFOLDER"
  dlg034="this script:\n\033[0;29m"
  dlg035="(i.e. \033[1;31m/home/user_name\033[1;34m) where \033[1;31muser_name\033[1;34m is the \033[1;31mname\033[1;34m of the user using"
  dlg036="${PS1}Please enter the \033[1;31mmain PATH\033[1;34m to set a new \033[1;31mHOME\033[1;34m for the ABCD installation folder"
  dlg037="The installation folder wasn't changed!"
  dlg038="${PS1}OK! Well! The new installation folder will be: \033[1;31m$HOME"
  dlg039="Do you want to continue: Y/n (empty return = No) ? "
  dlg040="You ended the script. Please restart it with the right install folder value!"
  dlg041="Good bye."
}
# --------------------------------------------------------------------------------

#
# function to get the installed language and set the messages accordingly.
#
ask_for_language() {
  TONGUE=$(echo $LANG|cut -c1,2)
  case $TONGUE in
    fr)
      set_language_fr;;
    *)
      set_language_us;;
  esac
}
# --------------------------------------------------------------------------------

#
# function to set the variables used in this script
#
set_variables() {
#
#  assign prompt colors
#
  RED="\033[01;31m"
  GREEN="\033[01;32m"
  YELLOW="\033[01;33m"
  BLUE="\033[01;34m"
  PINK="\033[01;35m"
  CIAN="\033[01;36m"
  WHITE="\033[01;37m"
  BACK="\033[0m"
#
#  get the user name
#
  ME=$USER
#
#  get the OS type
#
  SYSTEM=$(uname)
#
#  get the vendor name
#
  VENDOR=$(lsb_release -si)
#
#  get the distribution name
#
  NAME=$(lsb_release -sc)
#
#  get the distribution type (i.e. 32 or 64)
#
  FLAVOR=$(getconf LONG_BIT)
#
#  get the short version number (e.g. 12.04)
#
  RELEASE=$(lsb_release -sr)
#
#  get the full distribution version (e.g. 12.04.1)
#
  if [ "$VENDOR" = "Debian" ]
    then
      VERSION=$(lsb_release -sd|cut -d' ' -f3)
    else
      VERSION=$(lsb_release -sd|cut -d' ' -f2)
      LTS=$(lsb_release -sd|cut -d' ' -f3)
  fi
#
#  get the kernel number
#
  if [ "$VENDOR" = "Debian" ]
    then
      case $FLAVOR in
        '32')
          KERNEL=$(uname -r|sed 's/-686$//');;
        '64')
          KERNEL=$(uname -r|sed 's/-amd64$//');;
      esac
    else
      KERNEL=$(uname -r|sed 's/-generic$//')
  fi
#
#  define system file folders
#
  FOLDER1=/etc
  FOLDER2=/etc/security
  ABCDFOLDER="/ABCD_linux$FLAVOR-bit"
  EFGHFOLDER="/EFGH"
  TMPINSTALL="/temp_install-$FLAVOR-bit"
}
# --------------------------------------------------------------------------------

set_variables
ask_for_language
ask_for_root
dialog_intro

Si je lance ce script avec la commande : "./ABCD_install.sh", je reçoit un beau message d'erreur comme suit :

fnux@u-12-04:~$ ./ABCD_install.sh
./ABCD_install.sh: 129: ./ABCD_install.sh: 12.04\033[01;34m with ANSI C, ASM, C++, C#, Go,: not found
./ABCD_install.sh: 130: ./ABCD_install.sh: 12.04\033[01;34m!: not found
./ABCD_install.sh: 131: ./ABCD_install.sh: 12.04\033[01;34m can't be fully updated anymore!: not found
./ABCD_install.sh: 135: ./ABCD_install.sh: 12.04\033[01;34m but you won't: not found
./ABCD_install.sh: 136: ./ABCD_install.sh: 12.04\033[01;34m can't be updated anymore!: not found

WARNING
-------

This script can't be launched without the root privileges (i.e. sudo or su)!

Please restart the script with the command: sudo sh ABCD_install.sh
or use the su account and start: ./ABCD_install.sh

Thank you.

fnux@u-12-04:~$

Et si je le lance avec la commande "sudo ./ABCD_install.sh", j'obtiens alors le message d'erreur suivant :

fnux@u-12-04:~$ sudo ./ABCD_install.sh
./ABCD_install.sh: 129: ./ABCD_install.sh: 12.04\033[01;34m with ANSI C, ASM, C++, C#, Go,: not found
./ABCD_install.sh: 130: ./ABCD_install.sh: 12.04\033[01;34m!: not found
./ABCD_install.sh: 131: ./ABCD_install.sh: 12.04\033[01;34m can't be fully updated anymore!: not found
./ABCD_install.sh: 135: ./ABCD_install.sh: 12.04\033[01;34m but you won't: not found
./ABCD_install.sh: 136: ./ABCD_install.sh: 12.04\033[01;34m can't be updated anymore!: not found









fnux@u-12-04:~$ 

Donc, j'ai l'impression que mes variables de messages ne sont plus prises en compte par le script à partir du message dlg009.

J'en déduis (peut-être à tors) que le nombre de variables utilisables dans un script est limité.

Ma question est donc : comment augmenter le nombre de variables utilisables dans un script (si cela est possible bien sur).

Merci d'avance de toute suggestion car là, je suis vraiment bloqué.

Dernière modification par fnux (Le 23/05/2013, à 03:13)


N'engage pas un débat lors d'un dîner car celui qui n'a pas faim aura le dernier mot. - R. Whately
FWPBLA - The Froggies West Palm Beach Linux Association.

Hors ligne

#2 Le 23/05/2013, à 04:53

nesthib

Re : Big problème de variables avec BASH et/ou SH.

Ton point d'exclamation du message 10 est interprété comme une commande d'historique. Tu peux au choix désactiver ce comportement avec :

set +H

ou utiliser des guillemets simples :

echo '!'

GUL Bordeaux : GirollServices libres : TdCT.org
Hide in your shell, scripts & astuces :  applications dans un tunnelsmart wgettrouver des pdfinstall. auto de paquetssauvegarde auto♥ awk
  ⃛ɹǝsn xnuᴉꞁ uʍop-ǝpᴉsdn

Hors ligne

#3 Le 23/05/2013, à 04:57

MicP

Re : Big problème de variables avec BASH et/ou SH.

Bonjour fnux

fnux a écrit :

maintenant, j'aimerai rendre ce script multilingue

http://fr.wikipedia.org/wiki/Internatio … matique%29
http://traduc.org/L%27adaptation_des_logiciels/Gettext

Hors ligne

#4 Le 23/05/2013, à 06:41

tiramiseb

Re : Big problème de variables avec BASH et/ou SH.

J'ajouterais au message de MicP :
http://www.gnu.org/software/gettext/man … ripts.html



Et concernant l'erreur sur dlg009, tu as mis des guillemets sans les échapper :
Si on remplace le contenu par "XXX", tu as mis

dlg009="XXX"$VENDOR $RELEASE"XXX"

alors que ça devrait être:

dlg009="XXX\"$VENDOR $RELEASE\"XXX"

Pour dlg010, c'est la même chose mais il y a aussi le problème du point d'exclamation comme l'a dit nesthib. Et si tu mets des guillements simples autour de toute la ligne, ça n'interprétera pas les variables $VENDOR et $RELEASE, il faut donc décomposer en deux :

dlg010="XXX"$VENDOR $RELEASE"XXX!"

à remplacer par :

dlg010="XXX\"$VENDOR $RELEASE\"XXX"'!'



Mais gettext est LAAAAAAAAAAAARGEMENT mieux.

Dernière modification par tiramiseb (Le 23/05/2013, à 06:41)

Hors ligne