0

I have a snippet of code that works when used in its own .sh file:

#! /bin/bash
LastFileUpdate=`find $dir -name $SearchString -type f -printf '%T@ %p\n' | sort -n | tail -n 1| awk '{print $1}'`
echo $LastFileUpdate

This code works on its own, but when I insert it into another .sh script, it doesn't return anything! The variables are not used anywhere else. What could cause this?

let "Diff = $SystemTime - $LastFileUpdate"

This returns " let: Diff = 1111111 - : syntax error: operand expected(expected token is " ")

This implies the variable LastFileUpdate never even gets populated by the expression, which is wierd, because it works on it its own.

Ideas?

Edited by rectifryer: Duplicate, please remove

2
Contributors
4
Replies
14
Views
4 Years
Discussion Span
Last Post by rectifryer
1

Shell variables, including environment variables, are context sensitive. IE, you set a variable in one script, it (or its changes) are not visible outside of it after exit. If they are exported environmeent variables, then they are visible to scripts that IT calls, but not to the script that called it.

0

Instead of using separate scripts, use bash functions. Variables set there would be visible to other functions inside the same executing script.

0

Thanks rubberman, I reported this for deletion before I saw you reply because I figured it was in the wrong spot.

The code is all in the same script, I just isolated the first set to see if it would work and it does in its own script. When I insert it into the other script, it fails. It has no apparent dependencies AFAIK. In this case, this variable is only used for the lines in the example.

0

If I put double quotes around the $SearchString portion it works. What originally went there was:

Word_*

I replaced that with:

"Word_*"

Now it works and I do not know why.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.