Need smart script !

Hi,

I need a script to assign variables the below paths

 
/appl/user_projects/domains/<xxx>/servers/<yyy>/logs

Where <xxx> can be any number and name of directories.

<yyy> can be another set of any number of names and directories.

I want to neglect [do not wish to allocated them to variables] all the <xxx> and <yyy> folders that do not have the the complete path uptil [logs] directory as shown above.

can you give an example ? for exa our directorys

/appl/user_projects/domains/test.com/servers/dc1/logs
/appl/user_projects/domains/test.com/servers/dc2/logs
/appl/user_projects/domains/test.com/servers/dc3/logs

and

a1=test.com
b1=dc1
c1=dc2
......

like this ?

No... not like this.

I need this

/appl/user_projects/domains/test.com/servers/dc1/logs
/appl/user_projects/domains/test.com/config/dc6/logs
/appl/user_projects/domains/test.com/servers/dc2/logs
/appl/user_projects/domains/test.com/servers/dc3/dump

Now,

 
/appl/user_projects/domains/test.com/config/dc6/logs [does not have the "server" folder]

and

 
/appl/user_projects/domains/test.com/servers/dc3/dump [does not have a "logs" folder]

Hence both of them should be ignored.

This is what i need

 
a1=/appl/user_projects/domains/test.com/servers/dc1/logs
b1=/appl/user_projects/domains/test.com/servers/dc2/logs

Also, I need

dir1=dc1
dir2=dc2

Assuming that you want to process these directories one-by one we can create a while loop and only have one environment variable set per directory process.

ls -1d /appl/user_projects/domains/*/servers/*/logs 2>/dev/null | while read dir
do
       echo "${dir}"
       dc=$(basename $(dirname "${dir}"))
       echo $dc
       domain=$(basename $(dirname $(dirname $(dirname "${dir}"))))
       echo $domain
done

There are tidier ways of extracting the dc and domain name fields but it depends on what Shell you have.

1 Like