Bash script: problem with a function which use colors
Hello guys
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..
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..
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)
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)
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)
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)
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)
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)
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)
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)