Add new line after 'continue' in if statment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add new line after 'continue' in if statment
# 1  
Old 08-20-2018
Add new line after 'continue' in if statment

Hello,

im creating a csv file for email reporting on issues.
my problem is that after 'continue' command in if statment in a loop
the new paramter writing into the log doesnt take new line.


Code:
timeout 4s zabbix_get -I $ZBX -p $PORT -s $IP -k "system.run[grep "wheel" /etc/group]"

                if [ $? -eq 124 ]; then
                        echo -e "$RED TIMEOUT $NC"
                        sed -i "/$IP/s/$/ ,TIMEOUT/"  $LOG
                        continue
                fi

so if a good log that does not get TIMEOUT looks like that :
Code:
IP,Wheel user,Note
192.168.1.1,root someuser1 someuser2
192.168.1.2,root someuser1 someuser2

if some ip gets timeout and gets to the 'continue' the log is written like that :

Code:
IP,Wheel user,Note
192.168.1.1,root someuser1 someuser2192.168.1.2, ,TIMEOUT

i need to make the new writen line in the log file as a new line
or tell the sed command somthing.

to make it look like that :
Code:
192.168.1.1,root someuser1 someuser2
192.168.1.2, ,TIMEOUT

i tried to look for an answer but couldnt find.
and i cant just add new line as it make an empty line in csv.

thanks




Moderator's Comments:
Mod Comment Please use CODE tags correctly as required by forum rules!

Last edited by RudiC; 08-20-2018 at 04:49 AM.. Reason: Changed QUOTE to CODE tags.
# 2  
Old 08-20-2018
Is that reproducible or a sporadic behaviour?
Is that $LOG file being written to elsewhere?

What is that continue for?
# 3  
Old 08-20-2018
Quote:
Originally Posted by RudiC
Is that reproducible or a sporadic behaviour?
Is that $LOG file being written to elsewhere?

What is that continue for?
Hey,
LOG is simply a file /tmp/zabbix.csv that after the script i can atteched to email

continue acts like that -
if there is a time out on some IP it does no continue all the loop with this ip
and continue to next ip .

Code:
echo  "IP,Wheel users,Notes" | sudo tee -a $LOG

for IP in $HOST
	do
		echo -n "$IP, "  | sudo tee -a $LOG
		timeout 4s zabbix_get -I $ZBX -p $PORT -s $IP -k "system.run[grep "wheel" /etc/group]" 
		if [ $? -eq 124 ]; then
			echo -e "$RED TIMEOUT $NC" 
			sed -i "/$IP/s/$/ ,TIMEOUT/"  $LOG
			continue
		fi

as this script has more commands after that block
but if i get timeout there is no need for me to do all the loop with this ip
as ill get timeout 5 times. so its continueing to the next ip in the file $HOST
# 4  
Old 08-20-2018
Much clearer now.

Your echo -n suppresses the line terminator (to be added by the zabbix_get later), and the sed script doesn't add it either. Try adding a \n after the "TIMEOUT" string in your sed command.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search for word in huge logfile and need to continue to print few lines from that line til find date

Guys i need an idea for one logic..in shell scripting am struggling with a logic...So the thing is... i need to search for a word in a huge log file and i need to continue to print few more lines from that line and the consecutive line has to end when it finds the line with date..because i know... (1 Reply)
Discussion started by: Prathi
1 Replies

2. Shell Programming and Scripting

Wildcard in a tcsh if statment

Hello everyone I was hoping someone could tell me whether I can use a wildcard inside an tcsh if statement. I am trying to test the argument the user has fed the script and whether it is a .txt file. The Ides behind it is the following if (`echo $1` != *.txt) then echo "wrong... (6 Replies)
Discussion started by: smarones
6 Replies

3. Shell Programming and Scripting

Continue an instruction on more than one line in a script shell ?

Hello, I have a very long instruction to write, but, for lisibility reasons, I would like to cut it on more than one line and comment each lines. is it possible ? thanks :b: (1 Reply)
Discussion started by: shadok
1 Replies

4. Shell Programming and Scripting

Line in bash script to wait for x feedbacks and then continue

I have a script that runs a console/terminal command on the server and what is want is for each of the multiple success reports fed back from the clients (echo-ed out onto the conosle) to be counted and after x number of reports reboot the server. The Details: The command (program) is... (0 Replies)
Discussion started by: dp123
0 Replies

5. Shell Programming and Scripting

continue line in perl script

HI , I am new to the perl , I am using a if condition and in that if condition i am checking 7 variables value. so it continue to second line .And if i user "\" for the continue line it showing error. Example : if(a >9 || b>8 || c> 10 \ d > 11) { print(); } The above statement is... (3 Replies)
Discussion started by: julirani
3 Replies

6. UNIX for Dummies Questions & Answers

Comparing Special characters (i.e. -,\,/) in an if statment

I need to validate the special characters of a date (the characters between the year and month & month and day). The data filed is being populated by users and read into the script vi an argument. I want to ensure that the date is a '-' (dash) and not a '/' or '\' (slash). The every thing I... (3 Replies)
Discussion started by: angelap
3 Replies

7. Shell Programming and Scripting

Continue output redirection on the same line?

I have for i in 1 2 3 4 do echo $i >> test.txt done test.txt contains ... 1 2 3 4 But I want it to contain ... 1234 Any suggestions? (3 Replies)
Discussion started by: Bandit390
3 Replies

8. UNIX for Dummies Questions & Answers

Clarification for egrep statment

Can someone tell me what exactaly the following command is doing - pid_cmd="/usr/ucb/ps -axww | /usr/bin/egrep '${SUNMC2OSS_PATH}/SunMC2OSS\.jar.* sunmc2oss\.SunMC2OSS\$' | /usr/bin/egrep -v egrep | /usr/bin/nawk '{print \$1}'" Is the egrep is to check "sunmc2oss.SunMC2OSS" process inside... (2 Replies)
Discussion started by: puneet1983
2 Replies

9. Shell Programming and Scripting

if statment, based on retune value

hello im working on a project for the iphone to write a termainal based program we have bsd subsystem installed so have access to most unix command i have a executable called coordinates, which get the coordinates of the iphone when this runs it returns to the terminal the text of... (1 Reply)
Discussion started by: toliver182
1 Replies

10. Shell Programming and Scripting

what is wrong with my awk statment?

I am looking to find something in the hour(in field $2) of 03:00:07 and 04:00:07 and 05:00:07 and must contain something in field 4... why doesn't below command work? I try to use grep .. but since I am running this in loop, it's best I use the awk .. can someone please advise.. I am pretty... (1 Reply)
Discussion started by: hankooknara
1 Replies
Login or Register to Ask a Question