Basic bash, echo in loop for

Hi, I am trying to make a script to manage log. I want to write the name of the .gz I moved and the date :

for i  in `ls $replog/*.gz`  
do                     
  echo " $i "
  `echo $i date +%d:%m:%Y`  
  `echo $datee  `>> $replog/mrnet.log
  mv $i /var/log/vieux-logs
done

I need to echo the date in /$replog/mrnet.log, but when I want to echo $i I have a error message : "Permision denied"
I tryed to echo everything once at once, and its really the $i the matter

I don't know what to do

That is a Useless Use of ls. Try this:-

for i in $replog/*.gz
do
   echo "$i"
   echo "$i $( date +%d:%m:%Y )" >> $replog/mrnet.log
   mv "$i" /var/log/vieux-logs
done

you're true, the LS was useless, but I have the same problem :confused:

Are you still having issues? Can you post the error message in code tags if any?

Also make sure that you have write permission on file: $replog/mrnet.log

I did chmod 700 on mrnet.log, I still have the issues. My Linux is in french, and I don't know how to trap the code tags I can take you a screen shot if you want

Here is a forum video tutorial on how to use code tags.

BTW you can also post screenshot.

OMG!! I have no idea what that executable: /bin/mrnet is doing. I see there are several lines in it and it is throwing error @ lines 31, 35 @ 72 .

Your initial question was regarding how to log current date to some log file and the answer that I posted should work.

These errors seems to be totally different. So you have to DEBUG & figure them out.

Ok, I can send you the full code, the error you mentioned was function I din't need, I deleted it. I will send you a new screen shot with the error message. The comment are in french, and the echo that end with #trace is for debug

        

 #! /bin/bash
replog=/var/log
# V�rification du nombre d'entr�
if [ $# -eq 0 ] ; then
        echo "Bon nombres de param�tre entr� !" #A retir�       

        # V�rification si il y a bien des .gz
        ls -1 $replog/*.gz > /dev/null 2>&1
        if [ "$?" = "0" ]; then
                echo ".gz touvr� !!" # Trace



  #Cr�ation du rep vieux-logs si besoin
                if [ ! -d $replog/vieux-logs ] ; then
                        echo "vieux log n'existe pas ! cr�on le !" #trace
                        mkdir $replog/vieux-logs
                else
                        echo "Vieux log existe ! Une �tape de moin !" #trace
                fi

                #V�rification si mrnet.log existe
                if [ ! -f $replog/mrnet.log ] ; then
                        echo "Mrnet.log N'existe pas ! Cr�on le!" #trace
                        echo "Il ne faut pas oublier la fonction conteur !!!!" #trace
                        touch mrnet.log
                else
                        echo "Tout est baux, une �tape de moins ! pas de mrnet.log a cr��" #trace 
                fi


                #Boucle For
                for i  in  $replog/*.gz
                do
                        echo "Bravo, t'as comprit comment sa marche !" #trace
                        echo " $i "
                        `echo $i date +%d:%m:%Y` #this shit is fucked need help
                        `echo $datee  `>> $replog/mrnet.log
                        mv $i /var/log/vieux-logs
                done


                # Compar� 5k
                if [  `stat -c%s $replog/mrnet.log` -gt 5000  ]; then
                        echo "pipi caca pouet"
                else
                        echo "cacapipipouet"
                fi

        else
                echo "Pas de .gz trouv� :(" # TRace
        fi

else
        echo "Erreur Mauvais nombre de param�tre entr�" #trace
fi

---------- Post updated at 02:56 PM ---------- Previous update was at 02:56 PM ----------

Here are few changes that you can apply:-

touch $replog/mrnet.log                         # Provide absolute path
echo "$i $( date +%d:%m:%Y )"
echo "$( date +%d:%m:%Y )" >> $replog/mrnet.log # Remove backticks
mv "$i" /var/log/vieux-logs/                    # Remove backticks and wrap file name in double quotes

Apart from these changes, I do not see any syntax error which can cause this script to fail.

1 Like

You're my hero, it worked. So the matter was the `` needed to replace by "" and no echo in. Thanks ! If you have some time, I asked a question on the MV command on the same section of the forum!