如何在shell中获取父母的父目录名的基名?

时间:2017-05-31 17:33:32

标签: linux bash shell

我希望在shell脚本中获取当前目录的父目录的父目录的基本名称。

例如:

$ pwd
/home/tmp/terminal/env/1239754/control/state
$

我希望得到这个 - 1239754

3 个答案:

答案 0 :(得分:3)

我建议使用/* Asking the user if we can access their location */ func Authorization(){ if CLLocationManager.locationServicesEnabled() { switch CLLocationManager.authorizationStatus() { case .authorizedAlways: print("Authorized") case .authorizedWhenInUse: print("Authorized when in use") case .denied: displayAlertWithTitle("Not determined", message: "Location services are not allowed for this app") case .notDetermined: print("Not Determined") if let manager = self.locationManager { manager.requestWhenInUseAuthorization() } case .restricted: displayAlertWithTitle("Restricted", message: "Location services are not allowed for this app") } } else { print("Location services are not enabled") } } /* Location Detection */ func permission(){ if CLLocationManager.authorizationStatus() == .notDetermined { locationManager?.requestAlwaysAuthorization() } } override func viewDidAppear(_ animated: Bool){ super.viewDidAppear(animated) Authorization() } override func viewDidLoad() { super.viewDidLoad() self.locationManager = CLLocationManager() self.locationManager?.desiredAccuracy = kCLLocationAccuracyBest self.locationManager?.distanceFilter = 200 self.locationManager?.delegate = self } basename

dirname

答案 1 :(得分:0)

根据Cyrus和123的建议,这可能是最简单的解决方案,也适用于unicode角色。只需添加一个/ *

basename ${PWD%/*/*}

输出:

1239754

答案 2 :(得分:0)

你可以试试这个

basename `cd ../..; pwd`