導航:首頁 > 操作系統 > linuxshell當前目錄

linuxshell當前目錄

發布時間:2022-05-10 01:05:54

A. shell編程中,如何獲取當前執行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有可能是好幾種值,跟調用的方式有關系:

B. linux命令中,復制道當前目錄怎麼寫

1、連接上相應的linux主機,進入到等待輸入shell指令的linux命令行狀態下。

C. Linux如何使用shell查看目錄及其子目錄下的所有文件

執行tree命令即可按樹形結構輸出指定目錄下的所有子目錄和所有文件

D. linux Shell怎麼查找指定文件,並進入該文件所在目錄,目錄有空格

1.使用絕對路徑執行的shell文件(如/home/xxx/binfile)
直接使用dirname $0即可
2.對於使用相對路徑執行的shell文件(如 ./xxx/binfile)
pwd與dirname結合使用;pwd獲得的是執行當前shell文件時,用戶所在的位置;dirname可以獲得相對於那個位置的偏移:
例如某shell文件所在的位置是/home/user_name/work2/SNS3_server_im/Developing/trunk/im_capp/src/notify_serv/shell文件名

1 #!/bin/sh
2 pwd
3 echo `dirname $0`
執行後輸出
/home/user_name/work2/SNS3_server_im/Developing/trunk/im_capp/src
./notify_serv

E. linux下Shell編程:輸出當前目錄下所有文件,並輸出文件總數和目錄總數

#!/bin/sh

cd $1
file=`ls -a`
catlog=0
filenum=0

for loop in $file
do
if [ -d "$loop" ];then
catlog=`expr $catlog + 1`
else
filenum=`expr $filenum + 1`
echo "$loop"
fi
done

echo -e "the catlog number is $catlog"
echo -e "the catlog number is $filenum"

F. linux shell 打開執行目錄

可以在shell中定義變數,然後使用變數,實現高效的跳轉到對應目錄,免去了輸入較長的地址。
比如我裝的LAMP環境,每次跑起環境都挺麻煩的。具體命令如下:
cd /opt/lamppsudo ./manager.run Linux命令需求可查詢「Linux命令大全」。

G. 如何在linux中使用shell腳本遍歷指定目錄的文件,將創建時間大於指定時間的文件,復制到指定目錄下。

大於指定時間?最簡單的就是直接find裡面指定吧。例如,查找當前目錄及其子目錄所有mtime大於1天的文件:
find
/path
-type
f
-mtime
+1
即可,/path
可以換成其他路徑,-mtime
+1
表示時間大於1天。-1的話表示小於一天也就是1天之內的。

H. 在linux下編寫shell腳本,判斷當前目錄下所有文件類型,如果是普通文件,顯示內容,如果是目錄

forfilein*
do
if[-f"$file"]
then
if[-s"$file"]
then
printf"File:$file "
cat"$file"
else
rm"$file"
fi
else[-d"$file"]
printf"Directory:$file "
ls"$file"
fi
printf" "
done

I. 如何用linux命令進入一個目錄 並且執行該目錄下的一個文件

用linux命令進入一個目錄 並且執行該目錄下的一個文件方法如下:

1、打開文件vi(如/etc裡面有一個文件hosts,要打開這個文件執行vi hosts);

2、進入編輯模式i,a(打開hosts後不能進行編輯,輸入i之後出現---insert就進入編輯模式可以進行編輯了);

3、退出編輯模式,按下Esc;

4、進入命令模式,按下;

5、退出但不保存q;

6、退出保存wq或x;

7、保存w。

J. Linux Shell腳本如何獲取當前路徑

解答如下:

獲取腳本運行所在目標。常用三個方法,推薦第三個

#!/bin/bash
Cur_Dir="$(pwd)"
Cur_Dir="$(cd`dirname$0`;pwd)"
Cur_Dir="$(cd-P"$(dirname"${BASH_SOURCE[0]}")"&&pwd)"
echo${Cur_Dir}
cd${Cur_Dir}
閱讀全文

與linuxshell當前目錄相關的資料

熱點內容
密碼學的根基是加密 瀏覽:660
stata方差檢驗命令 瀏覽:335
解壓後文件夾里的內容丟失 瀏覽:713
解壓無敵視頻 瀏覽:688
什麼是伺服器辨認不了 瀏覽:126
java如何調用類方法 瀏覽:481
管理孩子的app叫什麼 瀏覽:544
壓縮活動軌跡 瀏覽:674
6米梁加密筋 瀏覽:79
怎麼學好ps如何學好編程 瀏覽:301
c編譯器廠商 瀏覽:114
簡述編譯程序以及解釋程序 瀏覽:3
linux升級kernel 瀏覽:176
入侵伺服器挖礦是什麼罪 瀏覽:47
房屋解壓資料丟了怎麼辦 瀏覽:810
java文件行讀寫 瀏覽:544
影城網上售票系統源碼 瀏覽:634
防疫就是命令歌曲 瀏覽:204
滴滴號碼加密怎麼解除 瀏覽:844
模具編程的職責 瀏覽:944