Version checking

Hi everyone,

I have binary file that call �app�, this file must be bind with �config file� to run.

problem is I have 10 different version of �app�, in different path with different �config file�!

This command show version of �app�:

root ~: /home/user1/app -version
2.7

The only way that i can get path of executable file and related config file is to run below command:

root ~: ps aux | grep app
/home/user1/app    bind          /home/user1/config1
/usr/app                   bind         /home/user5/config2
/tool/app                  bind         /opt/config3

...

Now I want to get list of version of each �app� that bind with config files, something like this:

  1. config1 2.7
  2. config2 4.9
  3. config3 1.2

Any idea?

Maybe you can use symbolic links to help you solve this problem?

First off: what a mess! My suggestion is to standardise these app versions first: create a directory for every version and put respective version plus its config file there. Something like this:

/opt/myapp/
            .../version1/
            .../version1/app
            .../version1/config
            .../version2/
            .../version2/app
            .../version2/config

To have a path like /tool is already questionable but to put an application there and its config into /opt is a sure-fire recipe for disaster. It may be a lot of work but you should clean that up as soon as possible.

If you have one config for several versions create one (maybe somewhere else) and use symbolic links like Neo suggested to represent them at the respective directory. Something like:

/opt/myapp/
            .../configs/
            .../configs/version1-3
            .../configs/version4-6
            .../version1/
            .../version1/app
            .../version1/config -> /opt/myapp/configs/version1-3
            .../version2/
            .../version2/app
            .../version2/config -> /opt/myapp/configs/version1-3
...
            .../version4/
            .../version4/app
            .../version4/config -> /opt/myapp/configs/version4-6

The application would still address these as file "config" in the same directory as the application.

I hope this helps.

bakunin

I need result export in text file not rename directory.

------ Post updated at 06:56 AM ------

I can't change location of files because they are running and I can't kill them.
I want to export result in text file , and don't want to create symbolic link or rename directory.

The following post-processes the ps output

#!/bin/sh
cnt=0
ps -eo args |
while read prog args
do
  case $prog in
  (*app);;
  (*)
    continue;;
  esac
  ver=$("$prog" -version </dev/null)
  echo "$((cnt+=1)) $prog $ver $args"
done
1 Like

not work!

Please be more specific: got the following error, got the following output instead of the following expected one.
--
I have corrected an error in my prev post.

Now with little modification it work perfectly! :slight_smile: Thank you
but have two question:
1-is it possible to set name for head columns?
2-in each config file wrote state, it it possible to show those state in new column like this:

No......config name......version......state
1...........config1...........2.7.........state1
2...........config2...........4.9.........state2
3............config3..........1.2.........state3

Thanks,

1) yes, for example you can simply echo the header line before the ps | while-do-done.

2) yes

Everything is possible. But cannot provide anything if you do not provide examples.

Sure this is example of my config1 file:

State = off
Hostname = rhel
Ip = 192.168.10.10

I want to show this items in the table like this:

No......config name......version............state.............ip.................hostname
1...........config1...........2.7...........off...........192.168.10.10..........rhel
2...........config2...........4.9...........on............192.168.10.11........ubuntu
3...........config3..........1.2............off...........192.168.10.12..........rhel
 

this code will made columns 1 2 3:

 #!/bin/sh
 cnt=0 ps -eo args | while read prog args
 do   

case $prog in   (*app);;   (*)     

continue;;   

esac   

ver=$("$prog" -version </dev/null)   

echo "$((cnt+=1)) $prog $ver $args" 

done

You can process a config file like this, put in a function:

readconfig(){
  # set state, hostname, ip
  while IFS=" =" read confkey confval
  do
    case $confkey in
    ([Ss]tate) state=$confval;;
    ([Hh]ostname) hostname=$confval;;
    ([Ii]p) ip=$confval;;
    esac
  done
}

You have given my script proposal that certainly needs some adaption.
YOUR version would be more helpful.
But maybe you can help yourself with the following information.
Once you have defined the readconfig function in your script, and once you have the pathname of a config file in a variable, say config , then you can call the function like this

  readconfig <$config

and then you have set the variables state , hostname , ip that you can echo out as $state , $hostname , $ip .

I try below code, but give me this error : ./script.sh: line 14: -bind /home/config: No such file or directory.
I think it is related to output of this command "ps -eo args",
"-bind /home/config"
consider "-bind" as argument, if I able to define only address of config file problem will be fix but i have no idea how can I extract only address of config file and suppress "-bind" !

#!/bin/sh
cnt=0
ps -eo args |
while read prog args
do
type=""
  case $prog in
  (*app);;
  (*)
    continue;;
  esac
 ver=$("$prog" -version </dev/null)

readconfig < $args
readconfig(){
  # set state, hostname, ip
  while IFS=" =" read confkey confval
  do
    case $confkey in
    ([Ss]tate) state=$confval;;
    ([Hh]ostname) host=$confval;;
    ([Ii]p) ip=$confval;;
    esac
  done
}

echo $confkey $confval $state $hostname $ip

Try

read prog TMP args

so -bind will be read into the TMP variable.

thank you this part run correctly and output are exact address. when i echo "$args" print address "/home/config", but part two not work correctly!

#!/bin/sh
cnt=0
ps -eo args |
while read prog tmp args
do
type=""
  case $prog in
  (*app);;
  (*)
    continue;;
  esac
 ver=$("$prog" -version </dev/null)

but this part not run and give me this error : ./script.sh: line 14: readconfig: command not found

readconfig < $args
readconfig(){
  # set state, hostname, ip
  while IFS=" =" read confkey confval
  do
    case $confkey in
    ([Ss]tate) state=$confval;;
    ([Hh]ostname) host=$confval;;
    ([Ii]p) ip=$confval;;
    esac
  done
}

echo $confkey $confval $state $hostname $ip

You need to define the function before you call it.

Sure, at first I define �args� and in section two call �args� and define �readconfig� , finally echo them but as I mention give me error! :frowning:

Take a look at your "section two" (for whatever reason you split your code in two). What is the sequence of readconf definition and reference?

Split to two section for simpler refering in our conversation.
Unfortunately can�t get your point, also search through the web
How to call a function in shell Scripting? - Stack Overflow

but didn�t help! :frowning:

readconfig(){
...
}
readconfig < $args

Try it but print empty lines!