Remplir un fichier de configuration avec sudo

Fermé
xunil2003 Messages postés 766 Date d'inscription mercredi 17 novembre 2004 Statut Membre Dernière intervention 29 septembre 2024 - 1 déc. 2023 à 23:37
mamiemando Messages postés 33401 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 28 novembre 2024 - 4 déc. 2023 à 16:03

Bonjour,

J'essai de créer un fichier bash de configuration avec sudo
 

1er essai

FICHIER="/etc/default/asterisk"
    #sudo bash -c "cat <<EOF>> $FICHIER
    # Startup configuration for the Asterisk daemon

    # Uncomment the following and set them to the user/groups that you
    # want to run Asterisk as. NOTE: this requires substantial work to
    # be sure that Asterisk's environment has permission to write the
    # files required  for  its  operation, including logs, its comm
    # socket, the asterisk database, etc.
    #AST_USER="asterisk"
    #AST_GROUP="asterisk"
    AST_USER="asterisk"
    AST_GROUP="asterisk"

    # If you DON'T want Asterisk to start up with terminal colors, comment
    # this out.
    COLOR=yes

    # If you want Asterisk to run with a non-default configuration file,
    # uncomment the following option, and set the value appropriately.
    #ALTCONF=/etc/asterisk/asterisk.conf

    # In the case of a crash, Asterisk may create a core file.  Uncomment
    # if you want this behavior.
    #COREDUMP=yes

    # Asterisk may establish a maximum load average for the system.  This
    # may be useful to prevent a flood of calls from taking down the system.
    #MAXLOAD=4

    # Or, if you'd prefer, you can limit the maximum number of calls.
    #MAXCALLS=1000

    # Default console verbosity.  This may be raised or lowered on the console.
    # Note this is analogous to the -v command line switch, which by default
    # will cause Asterisk to start in console mode and run in the foreground,
    # unless the always fork (-F) option is also provided.
    #VERBOSITY=3

    # Enable internal timing if the DAHDI timer is available.  The default
    # behaviour is that outbound packets are phase locked to inbound packets.
    # Enabling this option causes them to be locked to the internal DAHDI
    # timer instead.
    #INTERNALTIMING=yes

    # Start all recordings into a temporary directory, before moving them to
    # their final location.
    #TEMPRECORDINGLOCATION=yes
    #EOF"

bash me retourne :

bash: line 49: warning: here-document at line 1 delimited by end-of-file (wanted `EOF')

2em essai

    FICHIER="/etc/default/asterisk"
    
    sudo echo -e "
    
    # Startup configuration for the Asterisk daemon

    # Uncomment the following and set them to the user/groups that you
    # want to run Asterisk as. NOTE: this requires substantial work to
    # be sure that Asterisk's environment has permission to write the
    # files required  for  its  operation, including logs, its comm
    # socket, the asterisk database, etc.
    #AST_USER="asterisk"
    #AST_GROUP="asterisk"
    AST_USER="asterisk"
    AST_GROUP="asterisk"

    # If you DON'T want Asterisk to start up with terminal colors, comment
    # this out.
    COLOR=yes

    # If you want Asterisk to run with a non-default configuration file,
    # uncomment the following option, and set the value appropriately.
    #ALTCONF=/etc/asterisk/asterisk.conf

    # In the case of a crash, Asterisk may create a core file.  Uncomment
    # if you want this behavior.
    #COREDUMP=yes

    # Asterisk may establish a maximum load average for the system.  This
    # may be useful to prevent a flood of calls from taking down the system.
    #MAXLOAD=4

    # Or, if you'd prefer, you can limit the maximum number of calls.
    #MAXCALLS=1000

    # Default console verbosity.  This may be raised or lowered on the console.
    # Note this is analogous to the -v command line switch, which by default
    # will cause Asterisk to start in console mode and run in the foreground,
    # unless the always fork (-F) option is also provided.
    #VERBOSITY=3

    # Enable internal timing if the DAHDI timer is available.  The default
    # behaviour is that outbound packets are phase locked to inbound packets.
    # Enabling this option causes them to be locked to the internal DAHDI
    # timer instead.
    #INTERNALTIMING=yes

    # Start all recordings into a temporary directory, before moving them to
    # their final location.
    #TEMPRECORDINGLOCATION=yes
    
    #" >> $FICHIER

Il y a bien le fichier de rempli mais il ecrit :

AST_USER=asterisk
AST_GROUP=asterisk

au lieu de

AST_USER="asterisk"
AST_GROUP="asterisk"

3em essai

FICHIER="/etc/default/asterisk"
    
    sudo bash -c "echo \"
    # Startup configuration for the Asterisk daemon

    # Uncomment the following and set them to the user/groups that you
    # want to run Asterisk as. NOTE: this requires substantial work to
    # be sure that Asterisk's environment has permission to write the
    # files required  for  its  operation, including logs, its comm
    # socket, the asterisk database, etc.
    #AST_USER="asterisk"
    #AST_GROUP="asterisk"
    AST_USER="asterisk"
    AST_GROUP="asterisk"

    # If you DON'T want Asterisk to start up with terminal colors, comment
    # this out.
    COLOR=yes

    # If you want Asterisk to run with a non-default configuration file,
    # uncomment the following option, and set the value appropriately.
    #ALTCONF=/etc/asterisk/asterisk.conf

    # In the case of a crash, Asterisk may create a core file.  Uncomment
    # if you want this behavior.
    #COREDUMP=yes

    # Asterisk may establish a maximum load average for the system.  This
    # may be useful to prevent a flood of calls from taking down the system.
    #MAXLOAD=4

    # Or, if you'd prefer, you can limit the maximum number of calls.
    #MAXCALLS=1000

    # Default console verbosity.  This may be raised or lowered on the console.
    # Note this is analogous to the -v command line switch, which by default
    # will cause Asterisk to start in console mode and run in the foreground,
    # unless the always fork (-F) option is also provided.
    #VERBOSITY=3

    # Enable internal timing if the DAHDI timer is available.  The default
    # behaviour is that outbound packets are phase locked to inbound packets.
    # Enabling this option causes them to be locked to the internal DAHDI
    # timer instead.
    #INTERNALTIMING=yes

    # Start all recordings into a temporary directory, before moving them to
    # their final location.
    #TEMPRECORDINGLOCATION=yes
    
    \" > $FICHIER"

Idem, il y a bien le fichier de rempli mais il écrit :

AST_USER=asterisk
AST_GROUP=asterisk

au lieu de

AST_USER="asterisk"
AST_GROUP="asterisk"

Pouvez vous m'aider ?

Merci d'avance pour vos avis et conseils.

Linux / Firefox 120.0

A voir également:

3 réponses

avion-f16 Messages postés 19249 Date d'inscription dimanche 17 février 2008 Statut Contributeur Dernière intervention 15 juin 2024 4 505
2 déc. 2023 à 19:23

Bonjour,

Je te propose d'utiliser l'outil "tee" afin d'écrire le contenu vers le fichier spécifié.

L'écriture se faisant par le programme tee, le programme cat peut être exécuté normalement (sans privilège root).

Voici un exemple à adapter :

cat <<EOF | sudo tee /chemin/vers/fichier.txt

contenu...

EOF
0
xunil2003 Messages postés 766 Date d'inscription mercredi 17 novembre 2004 Statut Membre Dernière intervention 29 septembre 2024 14
3 déc. 2023 à 02:12

Bonjour,

ça ne vas pas, le texte est tout noir, il n'y a plus la couleur du texte, il y a un problème avec :

FICHIER="/etc/default/asterisk"
    
    cat <<EOF | sudo tee $FICHIER
    # Startup configuration for the Asterisk daemon

    # Uncomment the following and set them to the user/groups that you
    # want to run Asterisk as. NOTE: this requires substantial work to
    # be sure that Asterisk's environment has permission to write the
    # files required  for  its  operation, including logs, its comm
    # socket, the asterisk database, etc.
    #AST_USER="asterisk"
    #AST_GROUP="asterisk"
    AST_USER="asterisk"
    AST_GROUP="asterisk"

    # If you DON'T want Asterisk to start up with terminal colors, comment
    # this out.
    COLOR=yes

    # If you want Asterisk to run with a non-default configuration file,
    # uncomment the following option, and set the value appropriately.
    #ALTCONF=/etc/asterisk/asterisk.conf

    # In the case of a crash, Asterisk may create a core file.  Uncomment
    # if you want this behavior.
    #COREDUMP=yes

    # Asterisk may establish a maximum load average for the system.  This
    # may be useful to prevent a flood of calls from taking down the system.
    #MAXLOAD=4

    # Or, if you'd prefer, you can limit the maximum number of calls.
    #MAXCALLS=1000

    # Default console verbosity.  This may be raised or lowered on the console.
    # Note this is analogous to the -v command line switch, which by default
    # will cause Asterisk to start in console mode and run in the foreground,
    # unless the always fork (-F) option is also provided.
    #VERBOSITY=3

    # Enable internal timing if the DAHDI timer is available.  The default
    # behaviour is that outbound packets are phase locked to inbound packets.
    # Enabling this option causes them to be locked to the internal DAHDI
    # timer instead.
    #INTERNALTIMING=yes

    # Start all recordings into a temporary directory, before moving them to
    # their final location.
    #TEMPRECORDINGLOCATION=yes
    
    EOF

    echo "FICHIER : $FICHIER"
0
avion-f16 Messages postés 19249 Date d'inscription dimanche 17 février 2008 Statut Contributeur Dernière intervention 15 juin 2024 4 505
3 déc. 2023 à 14:59

Bonjour,

De quelles couleurs parles-tu ? C'est du texte brut, ça n'a pas de couleur.

Si tu parles de la couleur au niveau de ton éditeur de code, où tu écris ton code Bash : il est possible qu'il considère le texte "heredoc" comme une chaine de texte et qu'il n'y applique pas la coloration syntaxique Bash

Si tu parles au moment où tu ouvres le fichier /etc/default/asterisk : il faut utiliser un éditeur de texte qui ajoute la couleur pour les fichiers de configuration, c'est pas forcément activé par défaut avec nano ou vim.

Le contenu est-il bien inscrit dans /etc/default/asterisk ?

0
mamiemando Messages postés 33401 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 28 novembre 2024 7 804
Modifié le 4 déc. 2023 à 16:04

Bonjour,

Il y a plein de problèmes :

  • dans #1 : l'indentation est mauvaise, les guillemets ne sont pas échappés ;
  • dans #3 : ce n'est pas cat mais echo qu'il faut utiliser.

Voici comment tu peux procéder :

#!/bin/sh

chats="CHATS"
echo > fichier.txt << EOF "j'aime les chats
j'aime les $chats"
EOF
cat fichier.txt

Résultat : 

j'aime les chats
j'aime les CHATS

Bonne chance

0