Bash – Cannot cd to parent directory with cd dirname

bashsedshellweblogic

I have made a bash command which generates a one liner for restarting all Weblogic ( 8,9,10) instances on a server. The script works well but I need a suggestion to make it better using the dirname concept

/usr/ucb/ps auwwx | grep weblogic | tr ' ' '\n' | grep security.policy | grep domain | awk -F'=' '{print $2}' | sed 's/weblogic.policy//' | sed 's/security\///' | sort | sed 's/^/cd /' | sed 's/$/ ; cd .. ; \/recycle_script_directory_path\/recycle/ ;'  | tr '\n' ' '

To restart a Weblogic instance, the recycle ( /recycle_script_directory_path/recycle/) script needs to be initiated from within the domain directory as the recycle script pulls some application information from some .ini files in the domain directory.

The following part of the script generates a line to cd to the parent directory of the app i.e. the domain directory

sed 's/$/ ; cd .. ; \/recycle_script_directory\/recycle/ ;'  | tr '\n' ' '

I am sure there is a better way to cd to the parent directory like cd dirname but every time i run the following cd dirname command , it throws a "Variable syntax" error.

cd $(dirname '/domain_directory_path/app_name')

How do i incorporate the cd dirname in my bash command ?

Please let me know if there are any more enhancements I can do

Some info on my script

1) The following part lists out the weblogic instances running along with their full path

/usr/ucb/ps auwwx | grep weblogic | tr ' ' '\n' | grep security.policy | grep domain | awk -F'=' '{print $2}' | sed 's/weblogic.policy//' | sed 's/security\///' | sort

2) The grep domain part is required since all domain names have domain as the suffix

Best Answer

To solve your problem I wrote a very basic script :

#!/bin/bash
cd /usr/local/share/pixmaps
pwd 
sleep 5
path=(dirname '/usr/local/shar/pixmaps')
echo $path
sleep 5

the output of pwd is fine

the output of path is dirname

Thus when you try cd $(dirname '/domain_directory_path/app_name') it tries to go to cd dirname ... which apparently is not there

As I find from one of the blogs with an analogus situation :

$ cd $(dirname $(find / -name unknownfile.txt  2>/dev/null | head -n 1))
$ pwd
/home/peter/status/2007/november

Thus, I think cd $($(dirname '/domain_directory_path/app_name')) should solve your problem

I tried the following script :

#!/bin/bash
cd /usr/local/share/pixmaps
pwd 
sleep 5
path=($(dirname '/usr/local/share/pixmaps'))
echo $path
sleep 5
cd /
pwd
sleep 5
cd $path
pwd

and the output is

/usr/local/share/pixmaps
/usr/local/share
/
/usr/local/share

One more thing

Doing a direct cd $($(dirname '/domain_directory_path/app_name')) did not work

Thus, I guess you will first need to extract the path to some variable (path in my script) and then cd to it, as I have done in my script

Regards

amRit