Score:0

How can I stop displaying echo in a script

vu flag

I have a bash script which generates another script. I would like echo output to file only, but it displays on screen whatever I try (&> for example). Here is the code :

while read line; do echo "sudo ufw deny from "$line" to any"; done < /home/photobcdev/hack-ban.txt  >>/home/photobcdev/hack-ban.tmp

It works well, but displays hundreds lines every time it runs. How make it quiet ? Here is complete script for contextual information :

#!/bin/bash
DATE=$(date +%Y-%m-%d--%H-%M)
echo $(date)"   Suppression fichiers temporaires"
rm /home/photobcdev/hack.txt 2> /dev/null
rm /home/photobcdev/hack-ip.txt 2> /dev/null
rm /home/photobcdev/hack-ban.txt 2> /dev/null
rm /home/photobcdev/hack-ban.tmp 2> /dev/null
rm /home/photobcdev/hack-ban-diff.sh 2> /dev/null
echo $(date)"   Génération liste IPs"
sudo grep 'Disconnected from invalid user' /var/log/auth.log>>/home/photobcdev/hack.txt
grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' /home/photobcdev/hack.txt>>/home/photobcdev/hack-ip.txt
sort -u /home/photobcdev/hack-ip.txt>>/home/photobcdev/hack-ban.txt
wc -l /home/photobcdev/hack-ban.txt
echo $(date)"   Génération script ufw ban /IP"
#while read line; do echo "sudo ufw deny from "$line" to any"; done < /home/photobcdev/hack-ban.txt  >>/home/photobcdev/hack-ban.tmp
while read line; do
  echo "sudo ufw deny from $line to any" >> /home/photobcdev/hack-ban.tmp
  done < /home/photobcdev/hack-ban.txt
echo $(date)"   Création fichier final"
sort -u /home/photobcdev/hack-ban.tmp>>/home/photobcdev/hack-ban-${DATE}.sh
sort -u /home/photobcdev/hack-ban.sh
wc -l /home/photobcdev/hack-ban-${DATE}.sh
chmod +x /home/photobcdev/hack-ban-${DATE}.sh
grep -v -f /home/photobcdev/hack-ban.sh /home/photobcdev/hack-ban-${DATE}.sh >>/home/photobcdev/hack-ban-diff.sh
if [ -s /home/photobcdev/hack-ban-diff.sh ]; then
        # Fichier non vide
        echo $(date)"  Fichier hack-ban-diff.sh généré"
        wc -l /home/photobcdev/hack-ban-diff.sh
        rm -f /home/photobcdev/hack-ban.sh  2> /dev/null
        mv /home/photobcdev/hack-ban-${DATE}.sh /home/photobcdev/hack-ban.sh
        chmod +x /home/photobcdev/hack-ban-diff.sh
        echo $(date)"   Exécution hack-ban-diff.sh"
        /home/photobcdev/hack-ban-diff.sh
else
        # Fichier vide
        rm -f /home/photobcdev/hack-ban-diff.sh  2> /dev/null
        rm -f /home/photobcdev/hack-ban-${DATE}.sh  2> /dev/null
        echo $(date)"  Pas de nouvelles IP détectées"
fi
Raffa avatar
jp flag
Please run `set +x` in the terminal ... then retry your loop and see if it still prints output to the terminal
Alex Leguevaques avatar
vu flag
No change here.
Raffa avatar
jp flag
That is strange ... I can't imagine an error resulting from that `echo` line but try adding a space then `2> /dev/null` at the end of that line and fix the quoting i.e. `echo "sudo ufw deny from $line to any" 2> /dev/null` ... also please add some example lines from `hack-ban.txt` file if you can.
cn flag
That line should be redirecting all output from the loop into the output file. There must be something else in the script that is causing the extra output. Is there `set -x` or `exec` in your program?
Alex Leguevaques avatar
vu flag
There is no error in the script. I just want to suppress display output of this line. I could paste all script for more contextual information ?
cn flag
But that line **should not** be producing any output. What does the rest of the script look like? Is that tmp file populated as you expect?
Alex Leguevaques avatar
vu flag
no set -x or exec here . File is correctly generated. All works well except parasite display.
Raffa avatar
jp flag
"It works well, but displays hundreds lines every time it runs." ... Please add few examples of those lines ... Probably errors ... That's a complex script with some serious quoting issues ... How can you be so sure of the source command for the output you see?
Alex Leguevaques avatar
vu flag
``` sudo ufw deny from 93.190.106.139 to any sudo ufw deny from 94.101.186.12 to any sudo ufw deny from 94.139.166.33 to any sudo ufw deny from 94.23.155.237 to any sudo ufw deny from 95.0.15.234 to any sudo ufw deny from 95.213.158.26 to any sudo ufw deny from 95.217.101.214 to any sudo ufw deny from 96.239.59.131 to any sudo ufw deny from 96.78.175.39 to any sudo ufw deny from 98.142.142.201 to any sudo ufw deny from 98.252.188.193 to any sudo ufw deny from 99.130.111.161 to any```
Martin Thornton avatar
cn flag
Is that `sort -u /home/photobcdev/hack-ban.sh` meant to be there?
Alex Leguevaques avatar
vu flag
yes just to be sure hack-ban.sh and hack-ban-$date.sh are sorted identically so grep -v -f will work correctly.
hr flag
@AlexLeguevaques the point is that Line 21 `sort -u /home/photobcdev/hack-ban.sh` is **not** redirected to a file, so likely **that** is what is responsible for the terminal output
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.