Search code examples
linuxbashshelltelnet

Save continuous telegram in to different folder


I have a problem with saving output from the telegram. My problem is, from server I am getting data every minutes. My VERY SIMPLE CODE can save this continuous data into a .txt file.

But what I want is, to store this data in a daily folder. From this code, while running, it creates current day folder but keep adding next day data in the same folder and also in the same file.

My sample code is:

#!/bin/bash


foldername=$(date +%Y%m%d)
mkdir -p  /home/bash-test/"$foldername"

echolog(){
    if [ $# -eq 0 ]
    then cat - | while read -r message
        do
                echo "$(date -u +"%F %T %Z ; ") $message" | tee -a $MY_LOG
            done
    else
        echo -n "$(date -u +'%F %T %Z') ; " | tee -a $MY_LOG
        echo $* | tee -a $MY_LOG
    fi
}

telnet 197.204.12.253 1001 | echolog > /home/bash-test/"$foldername"/Ppt_$(date +%Y%m%d).txt

Solution

  • Oh now I've got it, 'echolog' is your function, I though it's some app at first sought. It could be simplified like this:

    #!/bin/bash
    
    fun(){
        while read data; do
            printf -v folder '%(%Y%m%d)T' -1
            printf -v filenm 'test_%(Y%m%d_%h%s)T.txt' -1
            mkdir -p $folder &> /dev/null
            echo "$data" >> "$folder/$filenm"
        done
    }
    
    tail -f file | fun