1. 程式人生 > >[轉]linux shell 獲取當前正在執行指令碼的絕對路徑

[轉]linux shell 獲取當前正在執行指令碼的絕對路徑

常見的一種誤區,是使用 pwd 命令,該命令的作用是“print name of current/working directory”,這才是此命令的真實含義,當前的工作目錄,這裡沒有任何意思說明,這個目錄就是指令碼存放的目錄。所以,這是不對的。你可以試試 bash shell/a.sh,a.sh 內容是 pwd,你會發現,顯示的是執行命令的路徑 /home/june,並不是 a.sh 所在路徑:/home/june/shell/a.sh

  另一個誤人子弟的答案,是 $0,這個也是不對的,這個$0是Bash環境下的特殊變數,其真實含義是:

   Expands to the name of the shell or shell script. This is set at shell initialization.  If bash is invoked with a file of commands, $0 is set to the name of that file. If bash is started with the -c option, then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the file name used to invoke bash, as given by argument zero.

   這個$0有可能是好幾種值,跟呼叫的方式有關係:

  • 使用一個檔案呼叫bash,那$0的值,是那個檔案的名字(沒說是絕對路徑噢)

  • 使用-c選項啟動bash的話,真正執行的命令會從一個字串中讀取,字串後面如果還有別的引數的話,使用從$0開始的特殊變數引用(跟路徑無關了)

  • 除此以外,$0會被設定成呼叫bash的那個檔案的名字(沒說是絕對路徑)

下面對比下正確答案:

basepath=$(cd `dirname $0`; pwd)

在此解釋下basepath :

dirname $0,取得當前執行的指令碼檔案的父目錄

cd `dirname $
0`,進入這個目錄(切換當前工作目錄) pwd,顯示當前工作目錄(cd執行後的)

由此,我們獲得了當前正在執行的指令碼的存放路徑。

相關推薦

[]linux shell 獲取當前正在執行指令碼絕對路徑

常見的一種誤區,是使用 pwd 命令,該命令的作用是“print name of current/working directory”,這才是此命令的真實含義,當前的工作目錄,這裡沒有任何意思說明,這個目錄就是指令碼存放的目錄。所以,這是不對的。你可以試試 bash shell/a.sh,a.sh 內

bash shell 獲取當前正在執行指令碼絕對路徑

如題,一般我們寫Shell指令碼的時候,都傾向使用絕對路徑,這樣無論指令碼在什麼目錄執行,都應該起到相同的效果,但是有些時候,我們設計一個軟體包中的工具指令碼,可能使用相對路徑更加靈活一點,因為你不知道使用者會在哪個目錄執行你的程式,就有了本文的題目。   常見的一種

Linux C 獲取當前應用程式的絕對路徑

1. 使用getcwd()獲取的是當前工作路徑,而不一定是程式的路徑 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define MAXBUFSIZE 1024

shell 獲取當前正在執行指令碼絕對路徑

#!/bin/bash basepath=$(cd `dirname $0`; pwd) echo $basepath basepath 解釋 : dirname $0      取得當前執行的指令碼檔案的父目錄 cd `dirname $0`   進入這個目錄(

linux C語言獲取當前執行程式的路徑及檔名 vs 獲取當前工作路徑

 獲取當前工作路徑: 標頭檔案:#include <unistd.h> 定義函式:char * getcwd(char * buf, size_t size); 函式說明:getcwd()會將當前的工作目錄絕對路徑複製到引數buf 所指的記憶體空間,引數size

Linux shell 獲取當前日期/時間

當前日期 [[email protected] DFJR-ERP]# cur_date=”date +%Y-%m-%d” [[email protected] DFJR-E

】C#獲取當前程式執行路徑的方法集合

//獲取當前程序的完整路徑,包含檔名(程序名)。 string str = this.GetType().Assembly.Location; result: X:\xxx\xxx\xxx.exe (.exe檔案所在的目錄+.exe檔名) //獲取新的 Process 元件並將其與當前活動的程序關聯的主模組的

C# ,asp.net 獲取當前,相對,絕對路徑

一、C#獲取當前路徑的方法: 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName -獲取模組的完整路徑。 2. System.Environment.CurrentDi

bash shell:獲取當前指令碼絕對路徑(pwd/readlink)

有時候,我們需要知道當前執行的輸出shell指令碼的所在絕對路徑,可以用dirname實現。 我們知道 dirname 可以獲取一個檔案所在的路徑,dirname的用處是: 輸出已經去除了尾部的”/”字元部分的名稱;如果名稱中不包含”/”, 則顯

如何在Windows和Linux獲取當前執行緒的ID號

Linux下獲取當前執行緒ID號函式: pthread_t pthread_self(); 返回:當前執行緒的ID號 pthread_t 資料型別的定義如下: typedef unsigned long int pthread_t; sizeof(pthread_t) =

linux c 獲取當前執行程序總數

獲取當前執行程序總數的命令為: ps auxw | wc -l 獲取當前執行程序總數的原始碼如下: #include <stdio.h> #include <stdli

linux shell自定義函數(定義、返回值、變量作用域)介紹

shel 自己 lai cell define ber article clas ner linux shell 可以用戶定義函數,然後在shell腳本中可以隨便調用。下面說說它的定義方法,以及調用需要註意那些事項。 一、定義shell函數(define function)

Linux獲取當前路徑所有目錄大小並排序

目錄排序 大小排序日常工作中,會遇到一堆目錄,如何查看各目錄大小呢,大家都知道du命令先列出前10條[email protected]/* */ /scripts]#du -sh * | head 120K 2017031510 248K 2017031514 23M 2017031515 664

Linux Shell獲取系統資源使用百分比(CentOS)

sta blog log grep iostat linu cut 內存使用率 dev CPU使用率: top -b -n 1 | head -n 4 | grep "^Cpu(s)" | awk ‘{print $2}‘ | cut -d ‘u‘ -f 1 內存使用率

Linux shell獲取之前的時間

minute utc時間 信息 shel 之前 lin 直接 輸入 是的 查詢有些信息是需要輸入一個start-time及end-time的,在Linux中,如果我想獲取五分鐘前的UTC時間該怎麽弄呢?獲取5分鐘前的標準UTC時間: date -d "5 minu

01: Shell概述 編寫及執行指令碼Shell變數 總結和答疑

Top NSD SHELL DAY01 案例1:Shell基礎應用 案例2:簡單Shell指令碼的設計 案例3:使用Shell變數 案例4:變數的擴充套件應用 1 案例1:Shell基礎應用 1.1 問題 本案例要求熟悉Linux Shell環境

C語言獲取當前編譯執行檔案(XXX.exe)檔名和目錄路徑

C語言獲取當前編譯執行檔案(XXX.exe)檔名和目錄路徑 vc2010編譯通過 #include<stdio.h> #include<string.h> #include<windows.h> #include<stdlib.h> int ma

jmeter 獲取執行指令碼路徑

需求:向jmeter.jmx 的路徑下 寫日誌 ; 1 import org.apache.jmeter.services.FileServer; 2 import com.bzj.utils.*; 3 String baseDir=FileServer.getFileServer().getBase

通過adb shell獲取當前視窗資訊

通過adb shell獲取當前視窗,目的是判斷是否有電話接入 獲取當前視窗資訊: adb shell dumpsys window windows | grep mCurrent  在鎖屏狀態:mCurrentFocus=Window{d4d7e33 u0 com.ss.and

用adb shell 獲取當前手機所有features

用adb shell 獲取當前手機所有features adb shell pm list features 在寫測試指令碼的時候會有用   這是從google的cts測試裡找出來的,androidxref上可以搜尋到 :androidxref 。 cts