導航:首頁 > 操作系統 > 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當前目錄相關的資料

熱點內容
青春解壓套餐是干什麼 瀏覽:435
素描教程pdf 瀏覽:716
pdfweb瀏覽 瀏覽:531
安卓開發怎麼進入物聯網 瀏覽:612
安卓手機絕地求生如何進入國際服 瀏覽:18
汽車活動廣告宣傳有什麼app 瀏覽:753
富二代f2抖音app怎麼玩 瀏覽:926
java線程啟動線程的方法 瀏覽:252
兩位數互補演算法 瀏覽:781
甲骨文雲伺服器登錄 瀏覽:776
程序員那麼可愛在哪裡可以免費觀看 瀏覽:566
台灣伺服器地址大全 瀏覽:192
安卓自動美顏怎麼關 瀏覽:960
java手動釋放內存 瀏覽:868
迷你世界怎麼自己創伺服器 瀏覽:811
cmd命令不能運行 瀏覽:298
最新pdf閱讀 瀏覽:756
c語言中的排序演算法 瀏覽:597
php原生鏈接資料庫 瀏覽:45
web前端後端程序員 瀏覽:25