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?

Recommended Answers

All 4 Replies

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.

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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.