Transpherer un fichier dans un tableau SH

Résolu
felaris Messages postés 11 Statut Membre -  
felaris Messages postés 11 Statut Membre -
Bonjour,

Je cherche a ecrire lecontenue d un fichier dans un tableau en Bourne shell. Cela fait maintenant 6h que je tourne en rond. N'ayant jamais fait de sh et etant presser par le temp je m en remet a vous. J ene comprend vraiment pas mon permission denied.

Ma variable $tab[] n'est utiliser que dans cette partie de code.

Voici mon code:

#!/bin/sh

size=$(cat sh.db | wc -l)
size=$((size+1))
echo size:$size
$tab[$size]

i=1
while [ $i != $size ]
do
line=$(head -$i sh.db | tail -1)
tab[$i]=$line
i=$((i+1))
done

i=1
while [ $i != $size ]
do
echo $tab[$i]
i=$((i+1))
done

Et voici ce que j obtien a l arriver:

size:11
tab[1]=tutu=sdgfsdgfg: Permission denied
tab[2]=tata=tutu: Permission denied
tab[3]=jhfuyhf=tutu: Permission denied
tab[4]=jhfuyhf=tutu: Permission denied
tab[5]=KGKGUGIU=lklklk: Permission denied
tab[6]=khkl=hfghg: Permission denied
tab[7]=kkgkjghk=kjgfuhgf: Permission denied
tab[8]=fhjfjhf=jhfhj: Permission denied
tab[9]=fhjfjhf=jhfhj: Permission denied
tab[10]=toto=tata: Permission denied
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]

le contenue du fichier est:

tutu=sdgfsdgfg
tata=tutu
jhfuyhf=tutu
jhfuyhf=tutu
KGKGUGIU=lklklk
khkl=hfghg
kkgkjghk=kjgfuhgf
fhjfjhf=jhfhj
fhjfjhf=jhfhj
toto=tata

je n ai le doit a aucune de ces fonctions:

expr
cp
mkdir
rmdir
touch
split
cmp
getopt
ed
awk
vi

Merci pour votre aide, bonne journee
Configuration: FreeBSD
Firefox 2.0.0.6

12 réponses

  1. jipicy Messages postés 40842 Date d'inscription   Statut Modérateur Dernière intervention   4 898
     
    Salut,
    jp@MDK:~/tmpfs ssh$ cat fich
    tutu=sdgfsdgfg
    tata=tutu
    jhfuyhf=tutu
    jhfuyhf=tutu
    KGKGUGIU=lklklk
    khkl=hfghg
    kkgkjghk=kjgfuhgf
    fhjfjhf=jhfhj
    fhjfjhf=jhfhj
    toto=tata
    
    jp@MDK:~/tmpfs ssh$ cat bar.sh
    #! /bin/sh
    
    #set -xv
    
    i=0
    while read line
    do
    tab[$i]=$line
    ((i++))
    done < fich
    
    j=0
    while [ "$j" -lt "${#tab[*]}" ]
    do
    echo "Element $((j+1)) du tableau : ${tab[$j]}"
    ((j++))
    done
    
    jp@MDK:~/tmpfs ssh$ ./bar.sh
    Element 1 du tableau : tutu=sdgfsdgfg
    Element 2 du tableau : tata=tutu
    Element 3 du tableau : jhfuyhf=tutu
    Element 4 du tableau : jhfuyhf=tutu
    Element 5 du tableau : KGKGUGIU=lklklk
    Element 6 du tableau : khkl=hfghg
    Element 7 du tableau : kkgkjghk=kjgfuhgf
    Element 8 du tableau : fhjfjhf=jhfhj
    Element 9 du tableau : fhjfjhf=jhfhj
    Element 10 du tableau : toto=tata
    
    jp@MDK:~/tmpfs ssh$
    0
  2. felaris Messages postés 11 Statut Membre
     
    Je vien de tester, malheureusement j obtient toujours un : permission denied au moment de l ecriture de la ligne dans le tableau.

    :s

    merci pour ton aide
    0
  3. lami20j Messages postés 21506 Date d'inscription   Statut Modérateur, Contributeur sécurité Dernière intervention   3 571
     
    Salut,

    Affiche tout simplement le résultat de
    cat sh.db 
    ls -l sh.db
    --
    106485010510997108
    0
  4. jipicy Messages postés 40842 Date d'inscription   Statut Modérateur Dernière intervention   4 898
     
    Décommentes la ligne "#set -xv" et affiche la sortie ici s'il te plaît.
    0
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. felaris Messages postés 11 Statut Membre
     
    Voici la sortie. Le code est porc mais ca marche jusqu a mon pb. J ai du legerement mof\difier le code que tu m a donner precedement pour cause de syntax error :s

    arg1=$1
    + arg1=put
    arg2=$2
    + arg2=toto
    arg3=$3
    + arg3=tat

    op_k_run=0
    + op_k_run=0
    op_f_run=0
    + op_f_run=0
    op_base_run=0
    + op_base_run=0
    error=0
    + error=0

    cmd="NULL"
    + cmd=NULL
    par1="NULL"
    + par1=NULL
    par2="NULL"
    + par2=NULL
    table="NULL"
    + table=NULL

    error_syntax()
    {
    echo "Syntax error : Usage : bdsh.sh [-k] [-f <db_file>] (put (<clef> | $<clef>) (<valeur> | $<clef>) |"
    echo " del (<clef> | $<clef>) [<valeur> | $<clef>] |"
    echo " select [<expr> | $<clef>] )"
    echo " flush )"
    exit 1
    }

    error_key()
    {
    echo "No such key : ..."
    exit 1
    }

    error_base()
    {
    echo "No base found : file ..."
    exit 1
    }

    do_put()
    {

    size=$(cat sh.db | wc -l)
    size=$((size+1))
    echo size:$size

    i=0
    while read line
    do
    tab[$i]=$line
    i=$((i+1))
    done < sh.db

    j=0
    while [ $j != $size ]
    do
    echo $tab[$j]
    j=$((j+1))
    done

    # var=$(grep $par1 sh.db)
    # line=$(grep $par1 sh.db | wc -l)
    # tmp=${var%=*}
    # echo TMP:$tmp
    # echo $line
    #
    # if [ $par1 = $tmp ]; then
    # {
    # echo "rgherherhe"
    # }
    # else
    # {
    # echo $par1=$par2 >> sh.db
    # }
    # fi
    exit 0
    }

    do_del()
    {
    exit 0
    }

    do_select()
    {
    exit 0
    }

    do_flush()
    {
    var=""
    > sh.db
    exit 0
    }

    ch_param()
    {
    if [ "put" = $cmd ]; then
    do_put
    fi
    if [ "del" = $cmd ]; then
    do_del
    fi
    if [ "select" = $cmd ]; then
    do_select
    fi
    if [ "flush" = $cmd ]; then
    do_flush
    fi
    echo "verification de param"
    }

    ##################check options

    if [ $1 ]; then
    {
    if [ "-k" = $1 ]; then
    op_k_run=1
    fi
    if [ "-f" = $1 ]; then
    op_f_run=1
    fi
    if [ $2 ] && [ "sh.db" = $2 ]; then
    op_base_run=1
    fi
    if [ "-f" = $2 ]; then
    op_f_run=1
    fi
    if [ $3 ] && [ "sh.db" = $3 ]; then
    op_base_run=1
    fi
    if [ 1 = $op_f_run ] && [ 0 = $op_base_run ]; then
    error=1
    fi
    if [ 0 = $op_f_run ] && [ 1 = $op_base_run ]; then
    error=1
    fi
    if [ $error = 1 ]; then
    error_syntax
    fi
    if [ "-f" != $1 ] && [ "-k" != $1 ] && [ "put" != $1 ] && [ "del" != $1 ] && [ "select" != $1 ] && [ "flush" != $1 ]; then
    error_syntax
    fi
    }
    fi
    + [ put ]
    + [ -k = put ]
    + [ -f = put ]
    + [ toto ]
    + [ sh.db = toto ]
    + [ -f = toto ]
    + [ tat ]
    + [ sh.db = tat ]
    + [ 1 = 0 ]
    + [ 0 = 0 ]
    + [ 1 = 0 ]
    + [ 0 = 1 ]
    + [ -f != put ]
    + [ -k != put ]
    + [ put != put ]

    ###############previous check command

    if [ 0 = $op_k_run ] && [ 0 = $op_f_run ]; then
    {
    if [ $1 ] && [ "select" = $1 ]; then
    {
    if [ 2 != $# ]; then
    error_syntax
    else
    {
    par1=$2
    cmd=$1
    ch_param
    }
    fi
    }
    fi
    if [ $1 ] && [ "del" = $1 ]; then
    {
    if [ 3 != $# ]; then
    error_syntax
    else
    {
    par1=$2
    cmd=$1
    ch_param
    }
    fi
    }
    fi
    if [ $1 ] && [ "put" = $1 ]; then
    {
    if [ 3 != $# ]; then
    error_syntax
    else
    {
    par1=$2
    par2=$3
    cmd=$1
    ch_param
    }
    }
    fi
    }
    fi
    if [ $1 ] && [ "flush" = $1 ]; then
    {
    if [ 1 != $# ]; then
    error_syntax
    else
    {
    cmd=$1
    ch_param
    }
    fi
    }
    fi
    if [ "put" != $1 ] && [ "del" != $1 ] && [ "select" != $1 ] && [ "flush" != $1 ]; then
    error_syntax
    fi
    }
    fi
    + [ 0 = 0 ]
    + [ 0 = 0 ]
    + [ put ]
    + [ select = put ]
    + [ put ]
    + [ del = put ]
    + [ put ]
    + [ put = put ]
    + [ 3 != 3 ]
    + par1=toto
    + par2=tat
    + cmd=put
    + ch_param
    + [ put = put ]
    + do_put
    + cat sh.db
    + wc -l
    + size= 10
    + size=11
    + echo size:11
    size:11
    + i=0
    + read line
    + tab[0]=tutu=sdgfsdgfg
    tab[0]=tutu=sdgfsdgfg: Permission denied
    + i=1
    + read line
    + tab[1]=tata=tutu
    tab[1]=tata=tutu: Permission denied
    + i=2
    + read line
    + tab[2]=jhfuyhf=tutu
    tab[2]=jhfuyhf=tutu: Permission denied
    + i=3
    + read line
    + tab[3]=jhfuyhf=tutu
    tab[3]=jhfuyhf=tutu: Permission denied
    + i=4
    + read line
    + tab[4]=KGKGUGIU=lklklk
    tab[4]=KGKGUGIU=lklklk: Permission denied
    + i=5
    + read line
    + tab[5]=khkl=hfghg
    tab[5]=khkl=hfghg: Permission denied
    + i=6
    + read line
    + tab[6]=kkgkjghk=kjgfuhgf
    tab[6]=kkgkjghk=kjgfuhgf: Permission denied
    + i=7
    + read line
    + tab[7]=fhjfjhf=jhfhj
    tab[7]=fhjfjhf=jhfhj: Permission denied
    + i=8
    + read line
    + tab[8]=fhjfjhf=jhfhj
    tab[8]=fhjfjhf=jhfhj: Permission denied
    + i=9
    + read line
    + tab[9]=toto=tata
    tab[9]=toto=tata: Permission denied
    + i=10
    + read line
    + j=0
    + [ 0 != 11 ]
    + echo [0]
    [0]
    + j=1
    + [ 1 != 11 ]
    + echo [1]
    [1]
    + j=2
    + [ 2 != 11 ]
    + echo [2]
    [2]
    + j=3
    + [ 3 != 11 ]
    + echo [3]
    [3]
    + j=4
    + [ 4 != 11 ]
    + echo [4]
    [4]
    + j=5
    + [ 5 != 11 ]
    + echo [5]
    [5]
    + j=6
    + [ 6 != 11 ]
    + echo [6]
    [6]
    + j=7
    + [ 7 != 11 ]
    + echo [7]
    [7]
    + j=8
    + [ 8 != 11 ]
    + echo [8]
    [8]
    + j=9
    + [ 9 != 11 ]
    + echo [9]
    [9]
    + j=10
    + [ 10 != 11 ]
    + echo [10]
    [10]
    + j=11
    + [ 11 != 11 ]
    + exit 0
    0
  7. lami20j Messages postés 21506 Date d'inscription   Statut Modérateur, Contributeur sécurité Dernière intervention   3 571
     
    Salut,

    Essaie avec tab[$i]="$line"
    0
  8. felaris Messages postés 11 Statut Membre
     
    Helas non ca ne passe toujours pas.
    J ai fait des chmod 777 sur tout mes fichier mais ca change rien
    0
  9. dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   5 660
     
    hello
    /bin/sh sur FreeBSD est-ce du Bourne shell ou du bash ?
    le Bourne shell original ne connait pas les tableaux.
    0
  10. felaris Messages postés 11 Statut Membre
     
    c du bourne shell s il connai pas les tableau et n ayant pas le droit a getopt et compagnie je voi pas comment realiser le proj :'(
    0
    1. dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   5 660
       
      avec #!/bin/ksh ?
      0
  11. felaris Messages postés 11 Statut Membre
     
    je n ai le droit a rien d autre que #!/bin/sh
    0
  12. dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   5 660
     
    que répond
    cat /etc/shells
    ls -l /bin/*sh*
    0
  13. felaris Messages postés 11 Statut Membre
     
    desoler pour l attente j ai eu quelque souci... passons.
    finalement je vai contourner le probleme en passant par un fichier temporaire dans lequel je redirige ce qui m interresse vis a vis de mon fichier sh.db puis ensuite une fois les modifications terminer, je redurige le contenu du fichier temporaire dans le fichier d origine que j aurai au prealable vider puis je supprime le fichier temporaire.

    Merci beaucoup pour votre aide.
    0