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 19/10/2024, à 20:51

xunil2003

Script bash avec la commande echo en root pour ecrire dans un fichier

Bonjour,

J'ai besoin de réaliser un script bash qui cré et rempli le fichier sur plusieur ligne.

le fichier doit contenir

#! /bin/bash
# scantoimage custom pour scantofile
#
mkdir -p ~/brscan
sleep 0.2

if [ -e ~/.brscan-skey/scantofile.config ];then
   source ~/.brscan-skey/scantofile.config
elif [ -e /etc//opt/brother/scanner/brscan-skey/scantofile.config ];then
   source /etc//opt/brother/scanner/brscan-skey/scantofile.config
fi


SCANIMAGE="/opt/brother/scanner/brscan-skey/skey-scanimage"
OUTPUT=~/brscan/Scan_"$(date +%Y-%m-%d-%H-%M-%S)".tif
OPT_OTHER=""


if [ "$resolution" != '' ];then
   OPT_RESO="--resolution $resolution" 
else
   OPT_RESO="--resolution 100" 
fi

if [ "$source" != '' ];then
   OPT_SRC="--source $source" 
else
   OPT_SRC="--source FB" 
fi

if [ "$size" != '' ];then
   OPT_SIZE="--size $size" 
else
   OPT_SIZE="--size A4" 
fi

if [ "$duplex" = 'ON' ];then
   OPT_DUP="--duplex"
   OPT_SRC="--source ADF_C" 
else
   OPT_DUP=""
fi
OPT_FILE="--outputfile  $OUTPUT"

OPT_DEV="--device-name $1"

OPT="$OPT_DEV $OPT_RESO $OPT_SRC $OPT_SIZE $OPT_DUP $OPT_OTHER $OPT_FILE"

if [ "$(echo "$1" | grep net)" != '' ];then
    sleep 1
fi

#echo  "$SCANIMAGE $OPT"
$SCANIMAGE $OPT

if [ ! -e "$OUTPUT" ];then
   sleep 1
   $SCANIMAGE $OPT
fi

sleep 1

if [ "$(which tiff2pdf 2>/dev/null)" != '' ];then
  APL=tiff2pdf
elif [ "$(which mogrify 2>/dev/null)" != '' ];then
#nécessite /etc/ImageMagick-6/policy.xml ligne coder domain read|write
#ou dans les versions plus récentes, mettre en commentaire la ligne qui
#empêche le traîtement des PDF : <!-- policy domain="coder" ... pattern="PDF" /> -->
  APL=mogrify
elif [ "$(which gimp 2>/dev/null)" != '' ];then
  APL=gimp
#elif   [ "$(which okular 2>/dev/null)" != '' ];then
#  APL=okular
#elif   [ "$(which xreader 2>/dev/null)" != '' ];then
#  APL=xreader
#elif   [ "$(which libreoffice 2>/dev/null)" != '' ];then
#  APL=libreoffice
#elif   [ "$(which atril 2>/dev/null)" != '' ];then
#  APL=atril
#elif   [ "$(which eom 2>/dev/null)" != '' ];then
#  APL=eom
else
  APL=evince
fi
if [ "$APL" = 'tiff2pdf' ];then
$APL -j -s "Scan Brother converti" -o ${OUTPUT%.*}.pdf "$OUTPUT" 2>/dev/null; rm -f "$OUTPUT"
elif [ "$APL" = 'mogrify' ];then
$APL -format pdf -compress jpeg "$OUTPUT" 2>/dev/null; rm -f "$OUTPUT"
elif [ "$APL" = 'gimp' ] || [ "APL" = 'evince' ];then
echo "$APL $OUTPUT  2>/dev/null" \;sleep 2\;rm -f "$OUTPUT" | bash &
fi

Commnt utiliser dans un script bash la commande echo en root pour ecrire dans un fichier avecl e chemin :  /opt/brother/scanner/brscan-skey/script/scantofile.sh

J'ai essayer avec ceci

sudo bash -c "echo \"
    #! /bin/bash
    # scantoimage custom pour scantofile
    #
    mkdir -p ~/brscan
    sleep 0.2

    if [ -e ~/.brscan-skey/scantofile.config ];then
    source ~/.brscan-skey/scantofile.config
    elif [ -e /etc//opt/brother/scanner/brscan-skey/scantofile.config ];then
    source /etc//opt/brother/scanner/brscan-skey/scantofile.config
    fi


    SCANIMAGE="/opt/brother/scanner/brscan-skey/skey-scanimage"
    OUTPUT=~/brscan/Scan_"$(date +%Y-%m-%d-%H-%M-%S)".tif
    OPT_OTHER=""


    if [ "$resolution" != '' ];then
    OPT_RESO="--resolution $resolution" 
    else
    OPT_RESO="--resolution 100" 
    fi

    if [ "$source" != '' ];then
    OPT_SRC="--source $source" 
    else
    OPT_SRC="--source FB" 
    fi

    if [ "$size" != '' ];then
    OPT_SIZE="--size $size" 
    else
    OPT_SIZE="--size A4" 
    fi

    if [ "$duplex" = 'ON' ];then
    OPT_DUP="--duplex"
    OPT_SRC="--source ADF_C" 
    else
    OPT_DUP=""
    fi
    OPT_FILE="--outputfile  $OUTPUT"

    OPT_DEV="--device-name $1"

    OPT="$OPT_DEV $OPT_RESO $OPT_SRC $OPT_SIZE $OPT_DUP $OPT_OTHER $OPT_FILE"

    if [ "$(echo "$1" | grep net)" != '' ];then
        sleep 1
    fi

    #echo  "$SCANIMAGE $OPT"
    $SCANIMAGE $OPT

    if [ ! -e "$OUTPUT" ];then
    sleep 1
    $SCANIMAGE $OPT
    fi

    sleep 1

    if [ "$(which tiff2pdf 2>/dev/null)" != '' ];then
    APL=tiff2pdf
    elif [ "$(which mogrify 2>/dev/null)" != '' ];then
    #nécessite /etc/ImageMagick-6/policy.xml ligne coder domain read|write
    #ou dans les versions plus récentes, mettre en commentaire la ligne qui
    #empêche le traîtement des PDF : <!-- policy domain="coder" ... pattern="PDF" /> -->
    APL=mogrify
    elif [ "$(which gimp 2>/dev/null)" != '' ];then
    APL=gimp
    #elif   [ "$(which okular 2>/dev/null)" != '' ];then
    #  APL=okular
    #elif   [ "$(which xreader 2>/dev/null)" != '' ];then
    #  APL=xreader
    #elif   [ "$(which libreoffice 2>/dev/null)" != '' ];then
    #  APL=libreoffice
    #elif   [ "$(which atril 2>/dev/null)" != '' ];then
    #  APL=atril
    #elif   [ "$(which eom 2>/dev/null)" != '' ];then
    #  APL=eom
    else
    APL=evince
    fi
    if [ "$APL" = 'tiff2pdf' ];then
    $APL -j -s "Scan Brother converti" -o ${OUTPUT%.*}.pdf "$OUTPUT" 2>/dev/null; rm -f "$OUTPUT"
    elif [ "$APL" = 'mogrify' ];then
    $APL -format pdf -compress jpeg "$OUTPUT" 2>/dev/null; rm -f "$OUTPUT"
    elif [ "$APL" = 'gimp' ] || [ "APL" = 'evince' ];then
    echo "$APL $OUTPUT  2>/dev/null" \;sleep 2\;rm -f "$OUTPUT" | bash &
    fi
    \" >> $FICHIER_SCRIPT_SCAN_FILE_BROTHHER-2"


    echo
    cat -n $FICHIER_SCRIPT_SCAN_FILE_BROTHHER-2

et il me retourne

laurent@vincent-I7-9700K:~/Langages/bash/ubuntu-20.04$ bash $HOME/Langages/bash/ubuntu-20.04/Installer-la-fonction-scan-directe-sur-le-pc-de-brother-pour-les-fichiers-pdf.sh: ligne 273: /dev/null \;sleep 2\;rm -f  | bash &

    fi
    " >> /opt/brother/scanner/brscan-skey/script/scantofile.sh-2: Aucun fichier ou dossier de ce type
laurent@vincent-I7-9700K:~/Langages/bash/ubuntu-20.04$ 

Merci d'avance pour vos avis et conseils.


- AMD R9 5950x 64 Go Ram / Intel Pentium I7 960 - 12 Go Ram - 6 T0 - Kubuntu 12.04 x64 / Kubuntu 14.04 x64 / Kubuntu 16.04 x64 / Kubuntu 18.04 x64 /  Kubuntu 20.04 x64 / Mint
- DreamPlug Multi Boot / Marvel 1.22 Ghz / Architecture ARM / 512 Mo Ram / Carte-sd 16 Go class 10 / 5 Watts / Debian Squeeze 2.6.39.4
- Raspberry PI 1 / Raspberry PI 2 / Raspberry PI 3 / Raspberry PI 4 : 4Gb
JE SUIS CHARLIE

Hors ligne

#2 Le 19/10/2024, à 20:57

sputnick

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

Avec un here-doc:

cat<<'EOF' >/opt/brother/scanner/brscan-skey/script/scantofile.sh
    #! /bin/bash
    # scantoimage custom pour scantofile
    #
    mkdir -p ~/brscan
    sleep 0.2

    if [ -e ~/.brscan-skey/scantofile.config ];then
    source ~/.brscan-skey/scantofile.config
    elif [ -e /etc//opt/brother/scanner/brscan-skey/scantofile.config ];then
    source /etc//opt/brother/scanner/brscan-skey/scantofile.config
    fi


    SCANIMAGE="/opt/brother/scanner/brscan-skey/skey-scanimage"
    OUTPUT=~/brscan/Scan_"$(date +%Y-%m-%d-%H-%M-%S)".tif
    OPT_OTHER=""


    if [ "$resolution" != '' ];then
    OPT_RESO="--resolution $resolution" 
    else
    OPT_RESO="--resolution 100" 
    fi

    if [ "$source" != '' ];then
    OPT_SRC="--source $source" 
    else
    OPT_SRC="--source FB" 
    fi

    if [ "$size" != '' ];then
    OPT_SIZE="--size $size" 
    else
    OPT_SIZE="--size A4" 
    fi

    if [ "$duplex" = 'ON' ];then
    OPT_DUP="--duplex"
    OPT_SRC="--source ADF_C" 
    else
    OPT_DUP=""
    fi
    OPT_FILE="--outputfile  $OUTPUT"

    OPT_DEV="--device-name $1"

    OPT="$OPT_DEV $OPT_RESO $OPT_SRC $OPT_SIZE $OPT_DUP $OPT_OTHER $OPT_FILE"

    if [ "$(echo "$1" | grep net)" != '' ];then
        sleep 1
    fi

    #echo  "$SCANIMAGE $OPT"
    $SCANIMAGE $OPT

    if [ ! -e "$OUTPUT" ];then
    sleep 1
    $SCANIMAGE $OPT
    fi

    sleep 1

    if [ "$(which tiff2pdf 2>/dev/null)" != '' ];then
    APL=tiff2pdf
    elif [ "$(which mogrify 2>/dev/null)" != '' ];then
    #nécessite /etc/ImageMagick-6/policy.xml ligne coder domain read|write
    #ou dans les versions plus récentes, mettre en commentaire la ligne qui
    #empêche le traîtement des PDF : <!-- policy domain="coder" ... pattern="PDF" /> -->
    APL=mogrify
    elif [ "$(which gimp 2>/dev/null)" != '' ];then
    APL=gimp
    #elif   [ "$(which okular 2>/dev/null)" != '' ];then
    #  APL=okular
    #elif   [ "$(which xreader 2>/dev/null)" != '' ];then
    #  APL=xreader
    #elif   [ "$(which libreoffice 2>/dev/null)" != '' ];then
    #  APL=libreoffice
    #elif   [ "$(which atril 2>/dev/null)" != '' ];then
    #  APL=atril
    #elif   [ "$(which eom 2>/dev/null)" != '' ];then
    #  APL=eom
    else
    APL=evince
    fi
    if [ "$APL" = 'tiff2pdf' ];then
    $APL -j -s "Scan Brother converti" -o ${OUTPUT%.*}.pdf "$OUTPUT" 2>/dev/null; rm -f "$OUTPUT"
    elif [ "$APL" = 'mogrify' ];then
    $APL -format pdf -compress jpeg "$OUTPUT" 2>/dev/null; rm -f "$OUTPUT"
    elif [ "$APL" = 'gimp' ] || [ "APL" = 'evince' ];then
    echo "$APL $OUTPUT  2>/dev/null" \;sleep 2\;rm -f "$OUTPUT" | bash &
    fi
EOF

'EOF' avec les guillemets, c'est pour ne pas évaluer les variables. Sans les guillemets, ça va évaluer les variables et afficher leur contenus


On ne peut pas mettre d'array dans un string!
https://sputnick.fr/

Hors ligne

#3 Le 19/10/2024, à 21:26

Watael

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

if [[ $1 =~ net ]]; then

et

if which tiff2pdf 2>/dev/null; then

which étant un programme externe, on le remplacera avec profit par type -p, qui est une commande interne.

à part ça,
le seul echo que je vois pipe des commandes à bash.
je ne vois pas de root
et je ne comprends pas l'intérêt de la chose.

Dernière modification par Watael (Le 19/10/2024, à 21:27)


Connected \o/
Welcome to sHell. · eval is evil.

Hors ligne

#4 Le 19/10/2024, à 22:12

xunil2003

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

Bonsoir,

ça ne fonctionne pas le texte reste noir il n'est plus colorié avec les couleurs du code bash avec kate

cat<<'EOF' >/opt/brother/scanner/brscan-skey/script/scantofile-2.sh
    sudo sh -c 'echo "
    #! /bin/bash
    # scantoimage custom pour scantofile
    #
    mkdir -p ~/brscan
    sleep 0.2

    if [ -e ~/.brscan-skey/scantofile.config ];then
    source ~/.brscan-skey/scantofile.config
    elif [ -e /etc//opt/brother/scanner/brscan-skey/scantofile.config ];then
    source /etc//opt/brother/scanner/brscan-skey/scantofile.config
    fi


    SCANIMAGE="/opt/brother/scanner/brscan-skey/skey-scanimage"
    OUTPUT=~/brscan/Scan_"$(date +%Y-%m-%d-%H-%M-%S)".tif
    OPT_OTHER=""


    if [ "$resolution" != '' ];then
    OPT_RESO="--resolution $resolution" 
    else
    OPT_RESO="--resolution 100" 
    fi

    if [ "$source" != '' ];then
    OPT_SRC="--source $source" 
    else
    OPT_SRC="--source FB" 
    fi

    if [ "$size" != '' ];then
    OPT_SIZE="--size $size" 
    else
    OPT_SIZE="--size A4" 
    fi

    if [ "$duplex" = 'ON' ];then
    OPT_DUP="--duplex"
    OPT_SRC="--source ADF_C" 
    else
    OPT_DUP=""
    fi
    OPT_FILE="--outputfile  $OUTPUT"

    OPT_DEV="--device-name $1"

    OPT="$OPT_DEV $OPT_RESO $OPT_SRC $OPT_SIZE $OPT_DUP $OPT_OTHER $OPT_FILE"

    if [ "$(echo "$1" | grep net)" != '' ];then
        sleep 1
    fi

    #echo  "$SCANIMAGE $OPT"
    $SCANIMAGE $OPT

    if [ ! -e "$OUTPUT" ];then
    sleep 1
    $SCANIMAGE $OPT
    fi

    sleep 1

    if [ "$(which tiff2pdf 2>/dev/null)" != '' ];then
    APL=tiff2pdf
    elif [ "$(which mogrify 2>/dev/null)" != '' ];then
    #nécessite /etc/ImageMagick-6/policy.xml ligne coder domain read|write
    #ou dans les versions plus récentes, mettre en commentaire la ligne qui
    #empêche le traîtement des PDF : <!-- policy domain="coder" ... pattern="PDF" /> -->
    APL=mogrify
    elif [ "$(which gimp 2>/dev/null)" != '' ];then
    APL=gimp
    #elif   [ "$(which okular 2>/dev/null)" != '' ];then
    #  APL=okular
    #elif   [ "$(which xreader 2>/dev/null)" != '' ];then
    #  APL=xreader
    #elif   [ "$(which libreoffice 2>/dev/null)" != '' ];then
    #  APL=libreoffice
    #elif   [ "$(which atril 2>/dev/null)" != '' ];then
    #  APL=atril
    #elif   [ "$(which eom 2>/dev/null)" != '' ];then
    #  APL=eom
    else
    APL=evince
    fi
    if [ "$APL" = 'tiff2pdf' ];then
    $APL -j -s "Scan Brother converti" -o ${OUTPUT%.*}.pdf "$OUTPUT" 2>/dev/null; rm -f "$OUTPUT"
    elif [ "$APL" = 'mogrify' ];then
    $APL -format pdf -compress jpeg "$OUTPUT" 2>/dev/null; rm -f "$OUTPUT"
    elif [ "$APL" = 'gimp' ] || [ "APL" = 'evince' ];then
    echo "$APL $OUTPUT  2>/dev/null" \;sleep 2\;rm -f "$OUTPUT" | bash &
    fi
    EOF

il me retourne

laurent@vincent-I7-9700K:~/Langages/bash/ubuntu-20.04$ bash $HOME/Langages/bash/ubuntu-20.04/Installer-la-fonction-scan-directe-sur-le-pc-de-brother-pour-les-fichiers-pdf.sh
/home/laurent/Langages/bash/ubuntu-20.04/Installer-la-fonction-scan-directe-sur-le-pc-de-brother.sh: ligne 426: avertissement : « here-document » à la ligne 274 délimité par la fin du fichier (au lieu de « EOF »)
/home/laurent/Langages/bash/ubuntu-20.04/Installer-la-fonction-scan-directe-sur-le-pc-de-brother.sh: ligne 427: erreur de syntaxe : fin de fichier prématurée
laurent@vincent-I7-9700K:~/Langages/bash/ubuntu-20.04$

- AMD R9 5950x 64 Go Ram / Intel Pentium I7 960 - 12 Go Ram - 6 T0 - Kubuntu 12.04 x64 / Kubuntu 14.04 x64 / Kubuntu 16.04 x64 / Kubuntu 18.04 x64 /  Kubuntu 20.04 x64 / Mint
- DreamPlug Multi Boot / Marvel 1.22 Ghz / Architecture ARM / 512 Mo Ram / Carte-sd 16 Go class 10 / 5 Watts / Debian Squeeze 2.6.39.4
- Raspberry PI 1 / Raspberry PI 2 / Raspberry PI 3 / Raspberry PI 4 : 4Gb
JE SUIS CHARLIE

Hors ligne

#5 Le 19/10/2024, à 22:16

Watael

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

le délimiteur de fin (ici EOF) doit ne pas être ailleurs qu'au début de la ligne.


Connected \o/
Welcome to sHell. · eval is evil.

Hors ligne

#6 Le 19/10/2024, à 22:30

xunil2003

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

Bonsoir,

Il y a un truc que je ne pige pas

Watael a écrit :

le délimiteur de fin (ici EOF) doit ne pas être ailleurs qu'au début de la ligne.

Mais pourtant il est en début de ligne (voir poste #4)
Merci.


- AMD R9 5950x 64 Go Ram / Intel Pentium I7 960 - 12 Go Ram - 6 T0 - Kubuntu 12.04 x64 / Kubuntu 14.04 x64 / Kubuntu 16.04 x64 / Kubuntu 18.04 x64 /  Kubuntu 20.04 x64 / Mint
- DreamPlug Multi Boot / Marvel 1.22 Ghz / Architecture ARM / 512 Mo Ram / Carte-sd 16 Go class 10 / 5 Watts / Debian Squeeze 2.6.39.4
- Raspberry PI 1 / Raspberry PI 2 / Raspberry PI 3 / Raspberry PI 4 : 4Gb
JE SUIS CHARLIE

Hors ligne

#7 Le 19/10/2024, à 23:06

Watael

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

il n'y est pas, non.

    elif [ "$APL" = 'gimp' ] || [ "APL" = 'evince' ];then
    echo "$APL $OUTPUT  2>/dev/null" \;sleep 2\;rm -f "$OUTPUT" | bash &
    fi
    EOF

Connected \o/
Welcome to sHell. · eval is evil.

Hors ligne

#8 Le 20/10/2024, à 00:03

xunil2003

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

Bonsoir,

Je ne comprend pas ce que vous voulez dire ?
Dans ce cas là ou faut il le placer le EOF , vous me dites "il n'y est pas, non."
exemple :

sudo cat <<EOF >/etc/somedir/foo.conf
# my config file
foo=bar
EOF

EOF est au même emplacement dans mon poste #04 en début et fin du code a insérer dans le fichier ?????
Dans mon fichier a partir de cette ligne tous le restant du code et noir avec Kate il n'est plus en couleur.

cat <<'EOF' > /opt/brother/scanner/brscan-skey/script/scantofile-2.sh

Merci.

Dernière modification par xunil2003 (Le 20/10/2024, à 00:13)


- AMD R9 5950x 64 Go Ram / Intel Pentium I7 960 - 12 Go Ram - 6 T0 - Kubuntu 12.04 x64 / Kubuntu 14.04 x64 / Kubuntu 16.04 x64 / Kubuntu 18.04 x64 /  Kubuntu 20.04 x64 / Mint
- DreamPlug Multi Boot / Marvel 1.22 Ghz / Architecture ARM / 512 Mo Ram / Carte-sd 16 Go class 10 / 5 Watts / Debian Squeeze 2.6.39.4
- Raspberry PI 1 / Raspberry PI 2 / Raspberry PI 3 / Raspberry PI 4 : 4Gb
JE SUIS CHARLIE

Hors ligne

#9 Le 20/10/2024, à 00:41

xunil2003

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

Bonsoir

j'ai trouver il faut mettre

cat <<<'EOF' > /opt/brother/scanner/brscan-skey/script/scantofile-2.sh

au lieu de

cat<<'EOF' >/opt/brother/scanner/brscan-skey/script/scantofile-2.sh

et il me retourne un problème de permission d'écriture dans le fichier

/home/laurent/Langages/bash/ubuntu-20.04/Installer-la-fonction-scan-directe-sur-le-pc-de-brother.sh: ligne 300: /opt/brother/scanner/brscan-skey/script/scantofile-2.sh: Permission non accordée
/home/laurent/Langages/bash/ubuntu-20.04/Installer-la-fonction-scan-directe-sur-le-pc-de-brother.sh: ligne 392: EOF : commande introuvable

idem avec

sudo cat <<<'EOF' > /opt/brother/scanner/brscan-skey/script/scantofile-2.sh

Merci.


- AMD R9 5950x 64 Go Ram / Intel Pentium I7 960 - 12 Go Ram - 6 T0 - Kubuntu 12.04 x64 / Kubuntu 14.04 x64 / Kubuntu 16.04 x64 / Kubuntu 18.04 x64 /  Kubuntu 20.04 x64 / Mint
- DreamPlug Multi Boot / Marvel 1.22 Ghz / Architecture ARM / 512 Mo Ram / Carte-sd 16 Go class 10 / 5 Watts / Debian Squeeze 2.6.39.4
- Raspberry PI 1 / Raspberry PI 2 / Raspberry PI 3 / Raspberry PI 4 : 4Gb
JE SUIS CHARLIE

Hors ligne

#10 Le 20/10/2024, à 00:50

Watael

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

non, c'est bien <<, qui est un here-doc
alors que <<< est un here-string.

regarde la différence entre les termes en italique dans le man.

fais un test simple :

cat <<eof >/tmp/hereDoc.out
   foo
   bar
   baz
eof
cat /tmp/hereDoc.out

Connected \o/
Welcome to sHell. · eval is evil.

Hors ligne

#11 Le 20/10/2024, à 01:34

xunil2003

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

Bonsoir

j'ai fait un test dans nouveau fichier

cat <<eof >/tmp/hereDoc.out
    foo
    bar
    baz
    x
eof

La c'est ok ça marche

Maintenant je fait le même test en changent la chemin

sudo cat <<eof >/opt/brother/scanner/brscan-skey/script/scantofile-2.sh
    foo
    bar
    baz
    x
eof

Il me retourne

test-script-bash.sh: ligne 5: /opt/brother/scanner/brscan-skey/script/scantofile-2.sh: Permission non accordée

et pourtant j'ai bien indiqué sudo en debut

sudo cat <<eof >/opt/brother/scanner/brscan-skey/script/scantofile-2.sh

Dossier  /opt/brother/scanner/brscan-skey/script/

laurent@vincent-I7-9700K:~$ ls -alh /opt/brother/scanner/brscan-skey/script/
total 36K
drwxr-xr-x 2 root root 4,0K oct.  19 23:38 .
drwxr-xr-x 3 root root 4,0K janv. 17  2023 ..
-rwxr-xr-x 1 root root 5,0K oct.  22  2020 scantoemail.sh
-rw-r--r-- 1 root root 2,5K oct.  19 23:42 scantofile-2.sh
-rwxr-xr-x 1 root root 1,2K oct.  22  2020 scantofile.sh
-rwxr-xr-x 1 root root 1,6K oct.  19 15:40 scantoimage.sh
-rwxr-xr-x 1 root root 1,6K oct.  19 15:13 scantoimage.sh.bak
-rwxr-xr-x 1 root root 1,3K oct.  22  2020 scantoocr.sh
laurent@vincent-I7-9700K:~$

Merci.

Dernière modification par xunil2003 (Le 20/10/2024, à 01:37)


- AMD R9 5950x 64 Go Ram / Intel Pentium I7 960 - 12 Go Ram - 6 T0 - Kubuntu 12.04 x64 / Kubuntu 14.04 x64 / Kubuntu 16.04 x64 / Kubuntu 18.04 x64 /  Kubuntu 20.04 x64 / Mint
- DreamPlug Multi Boot / Marvel 1.22 Ghz / Architecture ARM / 512 Mo Ram / Carte-sd 16 Go class 10 / 5 Watts / Debian Squeeze 2.6.39.4
- Raspberry PI 1 / Raspberry PI 2 / Raspberry PI 3 / Raspberry PI 4 : 4Gb
JE SUIS CHARLIE

Hors ligne

#12 Le 20/10/2024, à 01:43

Watael

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

je n'avais pas fait le test sur un répertoire sur lequel je n'ai pas les droits* :

cat <<eof | sudo tee "/opt/$tonFic"
   foo
   bar
   baz
eof
$ cat "/opt/$tonFic"
   foo
   bar
   baz
$
$ sudo cat <<eof >/opt/monTest
foo
bar
baz
eof
bash: /opt/monTest: Permission non accordée
$

ça n'ira pas plus loin : le fichier n'est pas créé.
la redirection (>) est exécutée en premier, et puisque le redirection émane de l'utilisateur... ça coince. smile
je ne sais pas pourquoi/comment tes fichiers sont créés

--
* c'est pourtant un problème connu

sudo write to file

Dernière modification par Watael (Le 20/10/2024, à 01:52)


Connected \o/
Welcome to sHell. · eval is evil.

Hors ligne

#13 Le 20/10/2024, à 02:19

xunil2003

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

Bonsoir,

Ok ça marche dans le fichier test en root.
J'ai réessayé dans mon fichier, je m'aperçois que je ne peut pas utilisé la tabulation (qui met le texte en noir) alors c'est la caca avec les cases (case $variable in) pour tabuler.
Obliger de mettre le text carrément à gauche de l’écran. je ne le savais pas !

Merci à vous, Bonne soirée.

Dernière modification par xunil2003 (Le 20/10/2024, à 02:27)


- AMD R9 5950x 64 Go Ram / Intel Pentium I7 960 - 12 Go Ram - 6 T0 - Kubuntu 12.04 x64 / Kubuntu 14.04 x64 / Kubuntu 16.04 x64 / Kubuntu 18.04 x64 /  Kubuntu 20.04 x64 / Mint
- DreamPlug Multi Boot / Marvel 1.22 Ghz / Architecture ARM / 512 Mo Ram / Carte-sd 16 Go class 10 / 5 Watts / Debian Squeeze 2.6.39.4
- Raspberry PI 1 / Raspberry PI 2 / Raspberry PI 3 / Raspberry PI 4 : 4Gb
JE SUIS CHARLIE

Hors ligne

#14 Le 20/10/2024, à 04:05

Watael

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

ce n'est pas normal.
montre-nous le script in extenso.

les tabulations, les espaces et les alinéas, ne sont pas un problème quand le dernier MOT délimiteur du here-doc (ici EOF) est au tout début de la ligne.


Connected \o/
Welcome to sHell. · eval is evil.

Hors ligne

#15 Le 21/10/2024, à 19:18

Tawal

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

xunil2003 a écrit :

Bonsoir,

ça ne fonctionne pas le texte reste noir il n'est plus colorié avec les couleurs du code bash avec kate

cat<<'EOF' >/opt/brother/scanner/brscan-skey/script/scantofile-2.sh
    sudo sh -c 'echo "
    #! /bin/bash
    # scantoimage custom pour scantofile
    #
   …

Normal ou pas tongue
Le shebang n'est pas bon, il y a un espace entre #! et /bin/bash


Le savoir n'a d’intérêt que si on le transmet.
Useless Use of Cat Award
Filenames and Pathnames in Shell: How to do it Correctly
À chaque problème sa solution, à chaque solution son moyen, si pas de moyen, toujours le problème !

Hors ligne

#16 Le 21/10/2024, à 19:34

sputnick

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

@xunil2003:
- il faut etre root (sudo -s)
- avant de copier coller, le dernier EOF doit obligatoirement être en début de ligne


On ne peut pas mettre d'array dans un string!
https://sputnick.fr/

Hors ligne

#17 Hier à 14:41

Tawal

Re : Script bash avec la commande echo en root pour ecrire dans un fichier

sputnick a écrit :

@xunil2003:
- il faut etre root (sudo -s)
- avant de copier coller, le dernier EOF doit obligatoirement être en début de ligne

On peut tout faire depuis un utilisateur ayant des droits sudo.

cat <<EOF | sudo tee /opt/script.sh
# Code ici
     # Avec indentation etc ...
     # Et le dernier EOF en tout début de ligne
EOF

Il y aura une demande de mot de passe utilisateur une fois le EOF saisi et entré.


Le savoir n'a d’intérêt que si on le transmet.
Useless Use of Cat Award
Filenames and Pathnames in Shell: How to do it Correctly
À chaque problème sa solution, à chaque solution son moyen, si pas de moyen, toujours le problème !

Hors ligne