bazzanella : Ecrire vers un fichier distant
mercredi 8 mai 2013 à 23:01Vous souhaitez écrire le contenu d’une variable contenant des double-quotes dans un fichier distant.
Vous avez une variable contenant des doubles quotes (« ) :
le résultat d’affichage étant :Bonjour "martin" et bienvenue
Vous souhaitez donc conserver les doubles quotes entourant votre login martin et écrire ce résultat dans un fichier distant :
- ssh -p 22 -i /home/user1/.ssh/id_dsa user1@remoteserver "echo '$chaine' > /home/user1/file1.txt"
ou plus pratiquement en écrivant une fonction :
function write_var_to_remotefile () { local var_param="${1}" local remotepathfile_param="${2}" if [ -z ${var_param} ]; then echo "write_var_to_remotefile : Argument 1 non fourni" exit 1; fi if [ -z ${remotepathfile_param} ]; then echo "write_var_to_remotefile : Argument 2 non fourni" exit 1; fi if [ -z ${PORT} ]; then echo "write_var_to_remotefile : Global variable PORT non fourni" exit 1; fi if [ -z ${PATHPRIVKEY} ]; then echo "write_var_to_remotefile : Global variable PATHPRIVKEY non fourni" exit 1; fi if [ -z ${REMOTESERVER} ]; then echo "write_var_to_remotefile : Global variable REMOTESERVER non fourni" exit 1; fi if [ -z ${REMOTEUSER} ]; then echo "write_var_to_remotefile : Global variable REMOTEUSER non fourni" exit 1; fi /usr/bin/ssh -p ${PORT} -i ${PATHPRIVKEY} ${REMOTEUSER}@${REMOTESERVER} "echo '${var_param}' > ${remotepathfile_param}" }
- function write_var_to_remotefile () {
- local var_param="${1}"
- local remotepathfile_param="${2}"
- if [ -z ${var_param} ]; then
- echo "write_var_to_remotefile : Argument 1 non fourni"
- exit 1;
- fi
- if [ -z ${remotepathfile_param} ]; then
- echo "write_var_to_remotefile : Argument 2 non fourni"
- exit 1;
- fi
- if [ -z ${PORT} ]; then
- echo "write_var_to_remotefile : Global variable PORT non fourni"
- exit 1;
- fi
- if [ -z ${PATHPRIVKEY} ]; then
- echo "write_var_to_remotefile : Global variable PATHPRIVKEY non fourni"
- exit 1;
- fi
- if [ -z ${REMOTESERVER} ]; then
- echo "write_var_to_remotefile : Global variable REMOTESERVER non fourni"
- exit 1;
- fi
- if [ -z ${REMOTEUSER} ]; then
- echo "write_var_to_remotefile : Global variable REMOTEUSER non fourni"
- exit 1;
- fi
- /usr/bin/ssh -p ${PORT} -i ${PATHPRIVKEY} ${REMOTEUSER}@${REMOTESERVER} "echo '${var_param}' > ${remotepathfile_param}"
- }
Original post of bazzanella.Votez pour ce billet sur Planet Libre.
Articles similaires
- bazzanella : Fonctions BASH (08/05/2013)