Sponsored Content
Top Forums Shell Programming and Scripting Problem with (Understanding) function Post 302993154 by drysdalk on Tuesday 7th of March 2017 10:05:57 AM
Old 03-07-2017
Hi,

The problem is that (unless I'm missing something here) your syntax, as it stands, isn't valid and so won't work at all.

For example, you have a line like this in your function:

return RETVAL

In Bash, the 'return' function can only return a single numeric value. It should be thought of as the exit value of the function, more or less. You can't return a full alphanumeric string, and so this approach isn't valid. No function in Bash can ever pass back a string by using 'return'.

Secondly, you are using syntax like this in your output:

echo -e $LZ"Test"

Here you're mixing up variables with functions. If you want to call the function 'LZ', you have to do it by essentially treating it as a command name (like 'Log' in my earlier example). Bash will always treat $LZ as the contents of a variable called 'LZ', and not as a function. So the value of it will never change and will always be the same, unless you subsequently change it yourself.

So again unless I'm missing something in your explanation (which I certainly grant I may very well be), then you are going to have to re-write this so that the function is defined correctly, and that your output is generated by calling it correctly.

If you feel there's something I've not grasped here or if you have any other info that might clear things up, then please let me know and I'll be happy to assist further.
 

10 More Discussions You Might Find Interesting

1. Filesystems, Disks and Memory

having problem in understanding namei module

can anyone give me some idea on unix filesystem namei's algorithsm (2 Replies)
Discussion started by: kangc
2 Replies

2. Shell Programming and Scripting

egrep understanding problem

Hi, Can anyone please let me know the meaning of this line,i am not able to understand the egrep part(egrep '^{1,2}).This will search for this combination in beginning but what does the values in {}signifies here. /bin/echo $WhenToRun | egrep '^{1,2}:$' >/dev/null (1 Reply)
Discussion started by: namishtiwari
1 Replies

3. Shell Programming and Scripting

Help needed for understanding a function

There is a function called start: start() { echo -n $"Sending Startup Email: " echo "${RESTARTBODY}" | mutt -s "${RESTARTSUBJECT}" ${EMAIL} RETVAL=$? if ; then touch ${LOCKFILE} success else failure fi echo return ${RETVAL} } Can anyone explain what the bold part of the... (3 Replies)
Discussion started by: proactiveaditya
3 Replies

4. Shell Programming and Scripting

Problem with the shell script for understanding

Can Anybody please tell me the meaning of the script: #!/bin/sh str=$@ echo $str | sed 's/.*\\//' exit 0 (6 Replies)
Discussion started by: nixhead
6 Replies

5. UNIX for Dummies Questions & Answers

Problem understanding Paths

If I don't explain my issue well enough, I apologize ahead of time, extreme newbie here to scripting. I'm currently learning scripting from books and have moved on to the text Wicked Cool Shell Scripts by Dave Taylor, but there are still basic concepts that I'm having trouble understanding. ... (10 Replies)
Discussion started by: Chasman78
10 Replies

6. Shell Programming and Scripting

Problem in understanding export uses

i am beginner in shell scripting. not able to understand what below line will do. PS1=${HOST:=Žuname -nŽ}"$ " ; export PS1 HOST below is the script #!/bin/hash PS1=${HOST:=Žuname -nŽ}"$ " ; export PS1 HOST ; echo $PS1 and i getting the below output Žuname -nŽ$ (25 Replies)
Discussion started by: scriptor
25 Replies

7. Shell Programming and Scripting

Problem on understanding the regexp command

Hi all, I'm not clear of this regexp command: regexp {(\S+)\/+$} $String match GetString From my observation and testing, if $String is abc/def/gh $GetString will be abc/def I don't understand how the /gh in $String got eliminated. Please help. Thanks (2 Replies)
Discussion started by: mar85
2 Replies

8. Shell Programming and Scripting

Problem in understanding debugging

Hi i was going through the script debugging technique. below example was given in the book. 1 #!/bin/sh 2 3 Failed() { 4 if ; then 5 echo "Failed. Exiting." ; exit 1 ; 6 fi 7 echo "Done." 8 } 9 10 echo "Deleting old backups,... (11 Replies)
Discussion started by: scriptor
11 Replies

9. Shell Programming and Scripting

Need help understanding a function

Hello, I recently started going in depth with the shell, so I started learning from Linux Shell Scripting CookBook, 2nd edition. I am at the first chapter atm, and the author tells to define a function in the ~/.bashrc. The function is below. prepend() { && eval $1=\"$2':'\$$1\" && export... (1 Reply)
Discussion started by: Vaseer
1 Replies

10. Shell Programming and Scripting

Understanding an example of perl map() function

Hello, I have many folders under which there is always a file with the same name, which contains the data I need to process later. A perl oneliner was borrowed perl -e 'print "gene_id\t", join("\t", map {/(.*)\//; $1} @ARGV),"\n";' *_test.trim/level.csvto make a header so that each column... (5 Replies)
Discussion started by: yifangt
5 Replies
Test::Log4perl(3pm)					User Contributed Perl Documentation				       Test::Log4perl(3pm)

NAME
Test::Log4perl - test log4perl SYNOPSIS
use Test::More tests => 1; # setup l4p use Log::Log4Perl; # do your normal Log::Log4Perl setup here use Test::Log4perl; # get the loggers my $logger = Log::Log4perl->get_logger("Foo::Bar"); my $tlogger = Test::Log4perl->get_logger("Foo::Bar"); # test l4p Test::Log4perl->start(); # declare we're going to log something $tlogger->error("This is a test"); # log that something $logger->error("This is a test"); # test that those things matched Test::Log4perl->end("Test that that logs okay"); # we also have a simplified version: { my $foo = Test::Logger->expect(['foo.bar.quux', warn => qr/hello/ ]); # ... do something that should log 'hello' } # $foo goes out of scope; this triggers the test. DESCRIPTION
This module can be used to test that you're logging the right thing with Log::Log4perl. It checks that we get what, and only what, we expect logged by your code. The basic process is very simple. Within your test script you get one or more loggers from Test::Log4perl with the "get_logger" method just like you would with Log::Log4perl. You're going to use these loggers to declare what you think the code you're going to test should be logging. # declare a bunch of test loggers my $tlogger = Test::Log4perl->get_logger("Foo::Bar"); Then, for each test you want to do you need to start up the module. # start the test Test::Log4perl->start(); This diverts all subsequent attempts Log::Log4perl makes to log stuff and records them internally rather than passing them though to the Log4perl appenders as normal. You then need to declare with the loggers we created earlier what we hope Log4perl will be asked to log. This is the same syntax as Test::Log4perl uses, except if you want you can use regular expressions: $tlogger->debug("fish"); $tlogger->warn(qr/bar/); You then need to run your code that you're testing. # call some code that hopefully will call the log4perl methods # 'debug' with "fish" and 'warn' with something that contains 'bar' some_code(); We finally need to tell Test::Log4Perl that we're done and it should do the comparisons. # start the test Test::Log4perl->end("test name"); Methods get_logger($category) Returns a new instance of Test::Logger that can be used to log expected messages in the category passed. Test::Logger->expect(['dotted.path', 'warn' => qr'this', 'warn' => qr'that'], ..) Class convenience method. Used like this: { # start local scope my $foo = Test::Logger->expect(['foo.bar.quux', warn => qr/hello/ ]); # ... do something that should log 'hello' } # $foo goes out of scope; this triggers the test. start Class method. Start logging. When you call this method it temporarly redirects all logging from the standard logging locations to the internal logging routine until end is called. Takes parameters to change the behavior of this (and only this) test. See below. debug(@what) info(@what) warn(@what) error(@what) fatal(@what) Instance methods. String of things that you're expecting to log, at the level you're expecting them, in what class. end() end($name) Ends the test and compares what we've got with what we expected. Switches logging back from being captured to going to wherever it was originally directed in the config. Ignoring All Logging Messages Sometimes you're going to be testing something that generates a load of spurious log messages that you simply want to ignore without testing their contents, but you don't want to have to reconfigure your log file. The simpliest way to do this is to do: use Test::Log4perl; Test::Log4perl->suppress_logging; All logging functions stop working. Do not alter the Logging classes (for example, by changing the config file and use Log4perl's "init_and_watch" functionality) after this call has been made. This function will be effectivly a no-op if the enviromental variable "NO_SUPRESS_LOGGING" is set to a true value (so if your code is behaving weirdly you can turn all the logging back on from the command line without changing any of the code) Selectivly Ignoring Logging Messages By Priority It's a bad idea to completely ignore all messages. What you probably want to do is ignore some of the trivial messages that you don't care about, and just test that there aren't any unexpected messages of a set priority. You can temporarly ignore any logging messages that are made by passing parameters to the "start" routine # for this test, just ignore DEBUG, INFO, and WARN Test::Log4perl->start( ignore_priority => "warn" ); # you can use the levels constants to do the same thing use Log::Log4perl qw(:levels); Test::Log4perl->start( ignore_priority => $WARN ); You might want to ignore all logging events at all (this can be used as quick way to not test the actual log messages, but just ignore the output. # for this test, ignore everything Test::Log4perl->start( ignore_priority => "everything" ); # contary to readability, the same thing (try not to write this) use Log::Log4perl qw(:levels); Test::Log4perl->start( ignore_priority => $OFF ); Or you might want to not ignore anything (which is the default, unless you've played with the method calls mentioned below:) # for this test, ignore nothing Test::Log4perl->start( ignore_priority => "nothing" ); # contary to readability, the same thing (try not to write this) use Log::Log4perl qw(:levels); Test::Log4perl->start( ignore_priority => $ALL ); You can also perminatly effect what things are ignored with the "ignore_priority" method call. This persists between tests and isn't autoically reset after each call to "start". # ignore DEBUG, INFO and WARN for all future tests Test::Log4perl->ignore_priority("warn"); # you can use the levels constants to do the same thing use Log::Log4perl qw(:levels); Test::Log4perl->ignore_priority($WARN); # ignore everything (no log messages will be logged) Test::Log4perl->ignore_priority("everything"); # ignore nothing (messages will be logged reguardless of priority) Test::Log4perl->ignore_priority("nothing"); Obviously, you may temporarly override whatever perminant BUGS
Logging methods don't return the number of appenders they've written to (or rather, they do, as it's always zero.) Changing the config file (if you're watching it) while this is testing / supressing everything will probably break everything. As will creating new appenders, etc... AUTHOR
Mark Fowler <mark@twoshortplanks.com> COPYRIGHT
Copyright 2005 Fotango Ltd all rights reserved. Licensed under the same terms as Perl itself. perl v5.14.2 2011-11-16 Test::Log4perl(3pm)
All times are GMT -4. The time now is 02:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy