Sponsored Content
Top Forums Shell Programming and Scripting Bash script: problem with a function which use colors Post 302989818 by Arnaudh78 on Wednesday 18th of January 2017 02:42:03 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Imager::Color::Float(3pm)				User Contributed Perl Documentation				 Imager::Color::Float(3pm)

NAME
Imager::Color::Float - Rough floating point sample color handling SYNOPSIS
$color = Imager::Color->new($red, $green, $blue); $color = Imager::Color->new($red, $green, $blue, $alpha); $color = Imager::Color->new("#C0C0FF"); # html color specification $color->set($red, $green, $blue); $color->set($red, $green, $blue, $alpha); $color->set("#C0C0FF"); # html color specification ($red, $green, $blue, $alpha) = $color->rgba(); @hsv = $color->hsv(); # not implemented but proposed $color->info(); DESCRIPTION
This module handles creating color objects used by Imager. The idea is that in the future this module will be able to handle color space calculations as well. A floating point Imager color consists of up to four components, each in the range 0.0 to 1.0. Unfortunately the meaning of the components can change depending on the type of image you're dealing with: o for 3 or 4 channel images the color components are red, green, blue, alpha. o for 1 or 2 channel images the color components are gray, alpha, with the other two components ignored. An alpha value of zero is fully transparent, an alpha value of 1.0 is fully opaque. METHODS
new This creates a color object to pass to functions that need a color argument. set This changes an already defined color. Note that this does not affect any places where the color has been used previously. rgba() This returns the red, green, blue and alpha channels of the color the object contains. info Calling info merely dumps the relevant color to the log. AUTHOR
Arnar M. Hrafnkelsson, addi@umich.edu And a great deal of help from others - see the "README" for a complete list. SEE ALSO
Imager(3), Imager::Color. http://imager.perl.org/ perl v5.14.2 2011-06-06 Imager::Color::Float(3pm)
All times are GMT -4. The time now is 01:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy