Problem with (Understanding) function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with (Understanding) function
# 1  
Old 03-07-2017
Problem with (Understanding) function

I have this code

Code:
#!/bin/bash

LZ () {
RETVAL="\n$(date +%Y-%m-%d_%H-%M-%S) --- "
return RETVAL
}

echo -e $LZ"Test"
sleep 3
echo -e $LZ"Test"

which I want to use to make logentrys on my NAS. I expect of this code that there would be output like

Code:
2017-03-07_11-00-00 --- Test

2017-03-07_11-00-03 --- Test

But I get (without the three seconds difference)
Code:
2017-03-07_11-00-00 --- Test

2017-03-07_11-00-00 --- Test

Who knows the solution of my problem? I thank you for every hint and help.
# 2  
Old 03-07-2017
Hi,

Maybe try something like this ?

Code:
#!/bin/bash

Log()
{
        timestamp=`/bin/date +%Y-%m-%d_%H-%M-%S`
        echo "$timestamp" "---" "$*"
        return 0
}

Log "Test"
sleep 3
Log "Test"

So have a "Log" function that exists to return as output any input that's passed to it, but with the timestamp prepended to it, basically.

Hope this helps.
# 3  
Old 03-07-2017
@drysdalk:
Thank you for your answer. Your example works as expected. Unfortunately your suggestion is a bit different from my desired solution ;-)

Beside one centralized script which defines $LZ I have very much scripts which have "log-commands" like

Code:
echo -e $LZ"Logentry XY"

I do not want to change every "log-command" in every script. So it would be very helpfull if I would have a function which gives back the log entry (compare to my example).
# 4  
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.
# 5  
Old 03-07-2017
I'm afraid that your "desired solution" can't be achieved in the way indicated in your posts. You need to differentiate between shell functions and shell variables. Confusingly, shells can have variables and functions with the same names. The output
Code:
2017-03-07_11-00-00 --- Test

is the result of expanding the LZ variable (defined elsewhere, NOT in the code snippet) but NOT calling the LZ () function. That also explains why there's no three second time increase. As functions return exit codes only, this mechanism can't be used to supply deliberate results. You need to print results to stdout, as drysdalk proposed, or capture them in a "command substitution" in the callers code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question