Bash script: problem with a function which use colors


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script: problem with a function which use colors
# 1  
Old 01-18-2017
Bash script: problem with a function which use colors

Hello guys Smilie

I've a some issue with a function which use the bash colors in my script.
An example :
Code:
#!/bin/bash

set -x

log_in(){    
    host="srv1"
    remote_files="log/"
    LOG_FILE="logfile"

    green='\033[0;32m'
    red='\033[0;31m'
    none='\033[0m'

    if [[ $1 = "red" ]]; then
    color_in_red=("${red}"$2"${none}")
        echo -e "$color_in_red"
        echo -e "$(date)" "$2" >> "${LOG_FILE}"
    elif [[ $1 = "green" ]]; then
    color_in_green=("${green}"$2"${none}")
        echo -e "$(date)" "$2" >> "${LOG_FILE}"
        echo -e "$color_in_green"
fi
}
log_in red ""$host" The read right is missing on \""$remote_files"\""

The problem is it write good in the logfile but in output it stop write from the first variable, I should be more clear with the output that I had :
Code:
rsync_user@vmtest1:/home/scripts/test$ ./test.sh
+ host=srv1
+ remote_files=log/
+ LOG_FILE=logfile
+ log_in green 'srv1 The read right is missing on "log/"'
+ green='\033[0;32m'
+ red='\033[0;31m'
+ none='\033[0m'
+ [[ green = \r\e\d ]]
+ [[ green = \g\r\e\e\n ]]
+ color_in_green=("${green}"$2"${none}")
++ date
+ echo -e 'Wed Jan 18 17:33:16 CET 2017' 'srv1 The read right is missing on "log/"'
+ echo -e '\033[0;32msrv1'
srv1
rsync_user@vmtest1:/home/scripts/test$

And in the logfile, it works :
Code:
rsync_user@vmtest1:/home/scripts/test$ cat logfile
Wed Jan 18 17:33:16 CET 2017 srv1 The read right is missing on "log/"

Then, all in my console become red or green.. until I type "ll" maybe another commands..

If someone can help me, I would appreciate. Smilie
# 2  
Old 01-18-2017
I think that color_in_red=("${red}"$2"${none}") this is incorrect, putting it in ( ) like that will make it an array. You should just do color_in_red="${red}$2${none}"

Also, if you're using BASH, you can avoid a lot of those echo -e's by just storing the binary values in the first place.

Also, you don't have to re-open the same logfile every time to print one line, you can open the logfile once and write to it as many times as you want:

Also, if you print the color ones to stderr, they will go straight to console and won't muck up your pipes, etc.

Code:
#!/bin/bash

exec 5>>logfile


logthis() {
    green=$'\033[0;32m' # The variables will hold actual escape sequence, not backslash-zero-three-three
    red=$'\033[0;31m'
    none=$'\033[0m'

    printf "%s: %s\n" "$(date)" "$1" >&5 # Print to file opened above into FD 5
    printf "%s%s%s\n" "$green" "$1" "$none" >&2 # To stderr
}


Last edited by Corona688; 01-18-2017 at 03:58 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-18-2017
It work perfectly ! thanks
This User Gave Thanks to Arnaudh78 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash function problem

I am trying to figure out why I am having a "problem" with some functions in a bash script I am running. The reason for air quoting is that the functions are working, they are just not displaying anything to screen when called from another function. Here's an example: function Create_Input {... (6 Replies)
Discussion started by: dagamier
6 Replies

2. Shell Programming and Scripting

Calling function from another bash script

I would like to call functions from another bash script. How can I do it? Some code More code (11 Replies)
Discussion started by: kristinu
11 Replies

3. Shell Programming and Scripting

Passing parameters to bash script function (or subroutine)

I've found a few posts regarding passing parameters to a function or subroutine, but for some reason when I try to run a command based on part with these parameters it's not working. If I have the function echo the parameters they show correctly so I believe they are being passed right but the... (2 Replies)
Discussion started by: withanh
2 Replies

4. Shell Programming and Scripting

Help with grep and read function in a BASH Script

I'm putting together a script that will search my mail archives for emails that meet certain criteria and output the files to a text file. I can manually cat that text file and pipe it into sendmail and it will work (i.e. cat /pathtofile/foo.txt | sendmail -t me@company.com) My script sends... (7 Replies)
Discussion started by: binary-ninja
7 Replies

5. Shell Programming and Scripting

bash script function parameters

I have a question about bash script. Can I create a function there that accept parameters like functions in program language? (2 Replies)
Discussion started by: programAngel
2 Replies

6. Shell Programming and Scripting

error when call function in bash script

Dear all, Could you please advice as I when call function i found the following error " refills: command not found" note that refills is function name. following also the function and how i call it function refills { echo "formatting refills and telepin" >> $log awk -F,... (20 Replies)
Discussion started by: ahmed.gad
20 Replies

7. UNIX for Advanced & Expert Users

AWK sub function curious problem under bash

I need to detect the number of pages in a print job when it is available so I can warn users when they try to print a report much larger than they expected. Sometimes they are trying to print 1000 page reports when they thought they were getting a 10 page report. Under linux I am scanning the... (5 Replies)
Discussion started by: Max Rebo
5 Replies

8. Shell Programming and Scripting

Run function from script over ssh (BASH)

Hi, Is there any cleaver way to run function from the bash scrip over ssh? For example: #!/bin/bash #Function 1 FN1 () { ls -l } #Main run ssh user@host FN1 exit 0 Yeah, I know it will not work, but I'm asking how to make it to work :) I'm suspecting that it would be... (1 Reply)
Discussion started by: columb
1 Replies

9. UNIX for Dummies Questions & Answers

Easy About Bash Script function

Hi to all, first of all,i am working on MINIX 3 OS. I want to create a bash script file,which will create a list of files(not directories) that have been modified one specific day (i.e today) under my home directory. thank you very much!! :) (8 Replies)
Discussion started by: kostis1904
8 Replies

10. Shell Programming and Scripting

colors in BASH, doubt with script

I was trying to see all combinations of foreground and background colors in BASH. This works fine for me, echo -e '\E[37;46m TEXT' but when I tried the below script, the variables i and j do not expand. #!/bin/sh for i in `seq 30 37` # foreground do for j in `seq 40... (4 Replies)
Discussion started by: jaduks
4 Replies
Login or Register to Ask a Question