2017-02-12 106 views
1
#====================script 5 -- ls reccurssive avec cd ======= 
#!/bin/bash 
exec 2>/dev/null # redirige stderr pour toute la suite 
# au cas ou le script est invoque sans argument $1 
# n'existe pas, la commande suivante devient cd . 
cd ${1:-.} # problem that i miss understood 
for i in * ; do 
if [ -d $i ] ; then 
echo "$PWD/$i/ <-- repertoire" 
$0 $i # le script s'invoque lui-même 
else 
echo $PWD/$i 
fi 
done 

================================ ===================

有人可以向我解釋這個CD $ {1: - 。}什麼是指如何使用它,如果有任何文章解釋這個

+1

你的問題已經包含在法文答案。 – Cyrus

回答

2

${a:-b}手段,如手冊中說明的那樣,使用$a如果它被定義,否則只是b

這裏的想法是,如果腳本收到參數,將會定義$1,腳本將cd指向該目錄。如果腳本沒有收到參數,${1-.}將擴展到提供的默認值.

由於.代表當前目錄和cd .是一個空操作,這基本上意味着,「cd$1如果可用,否則乾脆用腳本矣。」