在linux 中,如何获取指定目录的父目录呢?

我把该功能封装成了一个简单的函数:

 

 
  
  1. fadir()  
  2. {  
  3.     local this_dir=`pwd`  
  4.     local child_dir="$1" 
  5.     dirname "$child_dir" 
  6.     cd $this_dir  

应用:

比如我想获取 /opt/abc/whuang 的父目录,则在shell 脚本中 这样编写:

father_dir=`fadir "/opt/abc/whuang"`

echo "father directory is  $father_dir ."

注意:linux 中的函数的返回值只能是0-255,不能是负数或字符串。