I'm contending with messy system that employs a lot of bash scripts for most of its functions, and we found a bug I need to work around.
I figured that I can make it work by changing a variable, VAR1
that is set to A
depending from where it loads files, but in order to do this, I need to make the system aware of when certain process has opened a file that is within a subdirectory, and then change the variable, since it has only two ways to load those files, either from a directory /var/system/folder/file.ext
or from a subdirectory /var/system/folder/subfolder/file.ext
.
so my assumption was, whatever, just do something like ....
if [ ps -aux | grep /var/system/folder/*/*.*]; then
VAR1=B
for example, or something to that effect, and that will make it display an output with the pathname from its command line arguments whenever it opens a file within that subfolder, and thus make it change the variable. But... for some reason, grep doesn't seem to work with wildcards, and I've been trying for a while but I couldn't figure out how to make this work. I'd really appreciate some help.
CodePudding user response:
I'm assuming you want to get the path depth, and the reason for wildcards was because the exact names are unknown in advance (otherwise just use them).
if
LC_ALL=POSIX ps -fo args= aux |
grep -E '[[:blank:]]/var/system/folder/[^/] /[^/] \.[^.] $' >/dev/null 2>&1
then
VAR1=B
fi
.
matches any character and must be escaped to be used literally.
You could change [[:blank:]]
to cmd-name[[:blank:]]
or similar (but allow for command line options etc). ^cmd-name.*/var/system..etc$
will match the start and end of a commandline. Commands may sometimes be invoked with a full path /path/cmd
or ./cmd
.
If you're on Linux, look at the inofify-tools
package (inotifywait(1)
, inotifywatch(1)
). You can use these to trigger commands when events such as access or modify happen for a file or files in a directory.
CodePudding user response:
Finally got it working! a version of what Jerry said was the effective solution.
function checksubdir {
ps -aux | grep " /var/system/folder/[^/]*/" | grep process
}
if checksubdir;
then
VAR1=B
made it produce an output whenever the process
was opening a file from a subpath in /var/system/folder
, with the second piping making it display ONLY this process and no additional outputs that would confuse the function. and then it changed the variable correctly.