Sponsored Content
Top Forums UNIX for Advanced & Expert Users [BASH] Errormessages and Traps with a 'here-doc' Post 302963219 by sea on Saturday 26th of December 2015 02:14:30 AM
Old 12-26-2015
Power [BASH] Errormessages and Traps with a 'here-doc'

Happy holidays everyone Smilie

I'm trying to increase usabilty for my scripted project-, for that i wanted to apply an error message poping up upon every and any error (other than proper exit) indicating the enduser a uniform, and hopefully readable error messsage.

To achieve this, i wrote a msg_error function, and prepared some trap handling...
Code:
	msg_error() { # CALLER SCRIPT FUNCNAME LINENO [CONTENT]
	# Prints an error message and exits
	#
		caller="\$1" script="\$2" fname="\$3" lineno="\$4" 
		shift 4
		content=($(echo "\${@}"))
		
		printf '\r\t%s:\t%s\n' \\
			"$(gettext 'Caller of script')" "\$caller" \\
			"$(gettext 'Scriptname')" "\$script" \\
			"$(gettext 'Functionname')" "\$fname" \\
			"$(gettext 'Linenumber')" "\$lineno"
		for cont in "\${content[@]}";do
			printf '\t%s\n' "\$cont"
		done
		trap − INT QUIT TERM EXIT
		exit 1
	}

Code:
#
#	Traps & Error messages
#
	traps_save(){ saved_traps=\$(trap) ; export saved_traps ; }
	traps_restore() { eval \$saved_trap;}
	export -f msg_error traps_save traps_restore
	# Set and save basic error message traping, 
	trap "msg_error \${CALLER:-$0} \$0 \${FUNCNAME:-NON-FUNCTION} \$LINENO 'Args: \$@' " HUP INT QUIT ABRT KILL ALRM TERM ## 1 2 3 6 9 14 15
	traps_save	# Save initial traps

My issue isnt this... well at least not really..
My issue is the follow up, when callling a script with the same name (but which is intended to be executed, while this generated script is ment to be sourced).
Code:
0 ~/prjs/tui $ export LC_ALL=C
0 ~/prjs/tui $ ./make ; sudo ./make-install ; sh ./tuirc  ##(this is /etc/tuirc)
0 ~/prjs/tui $ bash -x $(which tuirc)  ## (this is /bin/tuirc)
+ tui-print -H 'TUI (0.9.4-inprogress), Copyright (c) 2015 by sea' 'Simon Arjuna Erat (sea), 08:02:02 2015-12-26'
/etc/tuirc: line 129: trap: TUI (0.9.4-inprogress), Copyright (c) 2015 by sea: invalid signal specification
/etc/tuirc: line 129: trap: Simon Arjuna Erat (sea), 08:02:02 2015-12-26' : invalid signal specification
<..>
1 ~/prjs/tui $ which tuirc
/usr/bin/tuirc

Code:
+ '[' -z 'Hello World' ']'
+ printf '\033[5G%s' 'Hello World'
    Hello World+ '[' '!' -z '' ']'
+ false
+ '[' -z '' ']'
+ printf '\033[107G\033[116G| #\033[0m\033[0m'
                                                                                                                   | #+ $'\342\210\222'
/usr/bin/tui-print: line 1: $'\342\210\222': command not found

1 ~/prjs/tui $ grep "\$\'" bin/tui-print
1 ~/prjs/tui $

0 ~/prjs/tui $ head -n2 $(which tui-print)
#!/usr/bin/env bash
#

Somehow the \$\' doesnt return the values expected, not even with grepping all files.
Code:
1 ~/prjs/tui $ grep "\$\'" bin/tui*
bin/tui-ftp:		NUM=$$
bin/tui-log:	NUM=$$
bin/tui-tar:				$showBar && tui-progress -rbm $size_tarball

So the search for one of the errrors ($'\212.... not found) became more complicated than expected...
Code:
cat -n /etc/tuirc | grep 129
   129		trap "msg_error ${CALLER:-$0} $0 ${FUNCNAME:-NON-FUNCTION} $LINENO '$@'" INT QUIT TERM 
						## HUP INT QUIT ABRT KILL ALRM TERM ## 1 2 3 6 9 14 15

I dont understand where the invalid signal spec comes from?
Specialy since calling the /etc/tuirc script by itself, it closes without error, but sourcing from other files, it raises one, in way i cant follow the reason.

Any ideas or advices please?
Thank you in advance.

Last edited by sea; 12-26-2015 at 03:32 AM..
 

10 More Discussions You Might Find Interesting

1. Solaris

SNMP traps

Hello I am trying to check that SNMP traps could be sent from one server to other . I am running this command from receiving server to see it can receive it /usr/sbin/snoop udp port 162 and on the sending end I am running this commad cst051 UDP D=162 S=1480 LEN=120 but i am... (1 Reply)
Discussion started by: Ajwat
1 Replies

2. UNIX for Dummies Questions & Answers

Traps and Interrupts

Well, I don't know where exactly to ask this doubt so I'm asking in the newbie section. I was reading about traps and interrupts when I thought of traps as something that cease the control of the OS from the user and interrupts that cease the control yet provide support for multitasking. Am I right... (3 Replies)
Discussion started by: Legend986
3 Replies

3. Infrastructure Monitoring

Sun Solaris 9 Traps Contain Multiple Alarms

Hi all, I have a Sun Solaris 9 box which acts as a firewall. Sometimes if multiple actions which cause snmp traps occur very close together, a single snmp trap that is sent containing the text for multiple alarms. I would like to prevent that and have a trap sent for each action which would... (0 Replies)
Discussion started by: g0ld2k
0 Replies

4. Infrastructure Monitoring

Shell Script - Generate SNMP Traps

Good morning to you all I´m kinda of a noob to scripting, and my knowledge is still very basic: anyway, I´ve developed a small .sh script with the following purpose: - it will check a result file, checking if it has any values, or if it´s empty - if it´s empty it will send an email What... (0 Replies)
Discussion started by: zarahel
0 Replies

5. Shell Programming and Scripting

Open and work on a .doc file with bash script

Hello there....unix users :) I hope you can help me with this: I need open a doc file or xls files and work this files whit a bash script. For example: Open a doc file and copy information from a txt file in this doc file or xls file. Is it possible? Sorry for my english...I'm chilean... (3 Replies)
Discussion started by: bobbasystem
3 Replies

6. Infrastructure Monitoring

SNMP Traps

Hi all, lately i managed to install SNMP agent on Solaris 9 & 10. The second objective is now to configure traps. However (since i'm totally new to the SUN world) i don't know a way of how to start. Can somebody help me with details? What is the first thing that you need to do to start trapping?... (3 Replies)
Discussion started by: Wizard_1979
3 Replies

7. Shell Programming and Scripting

Send traps from text file

Hello everyone! I have a text file with the next content: 2010-04-15 15:21:15 Minor 15605325 1531 Alarm name = IAD Equipment In Service location=iad01.tecnac.camino.illapel, Module=26 and I want send traps with the informationn of this file. How I can do this? Many... (3 Replies)
Discussion started by: bobbasystem
3 Replies

8. UNIX for Dummies Questions & Answers

SNMP Traps

Hi, We are using Zyrion Traverse to monitor all the servers. I like to set the traps on all of the Linux servers to send the traps to that servers. So for on the /etc/snmp/snmp.conf file, this is what I have... ### SNMP Traps ### trap2sink x.x.x.x # Traverse server My... (4 Replies)
Discussion started by: samnyc
4 Replies

9. Solaris

Solaris Zones: traps and pitfalls

Hi all Solaris Experts, What are the traps and pitfalls in using Solaris zones? Specifically, what tools, utilities, system calls don't work or work differently in a non-global zone? I'm new to zones and I've already encountered one difference which is very significant to me: orphan... (5 Replies)
Discussion started by: ad101
5 Replies

10. Linux

Snmptrapd is not receiving generated traps in centos 5.7

Hi, I tried to run snmptrapd from my customized service in centos 5.7 machine... the command used to start snmptrapd is .... /opt/snmpdemo/bin/snmptrapd -Le -c /opt/snmpdemo/etc/snmp/snmptrapd.conf -n -C -t -m "" And tried to trigger a trap... Trap got triggered but snmptrapd didn't... (0 Replies)
Discussion started by: sharathpadman
0 Replies
All times are GMT -4. The time now is 09:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy