Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
It would help if you showed how you're generating the cd $(dirname ...) part, since that command as you show it is correct and works correctly without error. – Dennis Williamson Mar 16 '10 at 14:03
As you can this part sed 's/$/ ; cd .. ; \/recycle_script_directory_path\/recycle/ ;' | tr '\n' ' ' takes care of cd $(dirname '/domain_directory_path/app_name') – Sharjeel Mar 16 '10 at 15:48
Exactly what are you doing that is giving you the "variable syntax" error? I don't get any error from cd $(dirname '/domain_directory_path/app_name'). Does this command leave you in /usr without an error?: cd $(dirname /usr/bin) – Dennis Williamson Mar 16 '10 at 16:24
cd $(dirname /usr/bin) leaves me in the same place where i was before with the "variable syntax" error – Sharjeel Mar 16 '10 at 16:42
What do these commands show?: echo $0, echo $SHELL, type -a bash, echo $BASH_VERSION, bash --version (does it match the previous command?), uname -a, set -o|grep posix. Does type -a cd show an alias or a function called "cd"? – Dennis Williamson Mar 16 '10 at 23:23

To solve your problem I wrote a very basic script :

cd /usr/local/share/pixmaps
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

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

I tried the following script :

cd /usr/local/share/pixmaps
sleep 5
path=($(dirname '/usr/local/share/pixmaps'))
echo $path
sleep 5
cd /
sleep 5
cd $path

and the output is


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



share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.