Linux bash関数、サブディレクトリにジャンプ

序文:

Linuxでは、カーネルコードを管理している同僚が、レイヤーごとのCDは面倒すぎると考えています。たとえば、インクルードディレクトリに移動する場合は、cd ./xx/xx/xx/xx/xx/xx/incldueを実行する必要があるため、スクリプトの作成を手伝いました。関数はサブディレクトリに直接ジャンプできます。

関数

function cdx() {

	local szroot=.
	local sztarg=""
		if [ $# -gt 1 ]; then
		if [ -d $1 ]; then szroot=$1; sztarg=$2; else szroot=$2; sztarg=$1; fi
		elif [ $# -gt 0 ]; then
	 	sztarg=$1;
	fi
	if [ "x$sztarg" = "x" ]; then
		echo "Usage: zz root-directory target-directory"	
	else
		sztmp=$(echo $sztarg | awk '{s=$0; do{match(s,/\//); if(RLENGTH>0){s=substr(s,RSTART+1);}}while(RLENGTH>0); printf(s); }')
			slist=$(find $szroot -type d -name "*$sztmp" | grep "$sztarg$");
		total=0;
		sztmp="";
			for s in $slist;
			do
			sztmp=$s; total=$(expr $total "+" 1);
			done
		if [ $total -gt 1 ]; then
			for s in $slist; do echo $s | grep --color "$sztarg"; done
		else
			if [ -d $s ]; then cd $sztmp; fi
		fi
	fi
}

使用法:

vi ~/.bash_profile  # 编辑配置文件
# 将上述函数贴入.bash_profile
# 保存,然后,刷新
source ~/.bash_profile

実施した

cdx [root-directory] target-directory

例:

cdx ~/3.9/kernel x86

おすすめ

転載: blog.csdn.net/hylaking/article/details/86062164