目的:
一個路徑轉換的函數(shù):方便大家在aws 里面進行l(wèi)inux和windows 路徑的轉換虱歪;
(使用上一定注意加上引號cdwin "Z:\bgcrh\build\training_project_1\practice_wh\cp\tools"
)
參考資料:
https://www.coder.work/article/176623 轉換函數(shù)
https://www.cnblogs.com/zyh9900010/p/11748179.html sed 基本用法: 包括# 用法击纬;
https://blog.csdn.net/Jcggnice/article/details/108897392 sh腳本添加幫助文檔;
https://www.cnblogs.com/struggle-1216/p/13174266.html 函數(shù)定義
字符串判斷用[[ ]] || [[]] ; 數(shù)字判斷使用(( ))
https://cloud.tencent.com/developer/article/1414363 零度斷言
https://www.cnblogs.com/garyzhuang/p/10136852.html 腳本模板
https://www.tutorialspoint.com/how-to-output-colored-text-to-a-linux-terminal 顏色編碼
echo -e "\033[1;31mThis is bold red text\033[0m\n"
https://unix.stackexchange.com/questions/404586/how-to-save-a-function-in-bash-for-later-use 保存函數(shù)
測試
1. 輸入例子
- Windows: Z:\bgcrh\build\training_project_1\practice_wh\cp\tools
- Linux: /usrfiles/bgcrh/build/training_project_1/practice_wh/cp/tools
2. 源碼
#琼腔!/bin/bash
#####################################################################
# Program: cdwin win2linux linux2win
# Programmer: Kai Cao
# Date: 10Sep2021
# Study: all study
# Version: 1.0
# Input : Linux or Win Path
#
# Description & Uasge:
# 1.cdwin --> change directory by win path
# cdwin "Z:\bgcrh\build\training_project_1\practice_wh\cp\tools"
#
# 2.win2linux --> convert win path to linux Path
# win2linux "Z:\bgcrh\build\training_project_1\practice_wh\cp\tools"
#
# 3.linux2win --> convert linux path to win Path
# linux2win "/usrfiles/bgcrh/build/training_project_1/practice_wh/cp/tools"
#
#
###################################################################
# MODIFICATIONS:
# Programmer:
# Date:
# Reason:
#
###################################################################/
####################################### cdwin ##########################################
# cdwin "Z:\bgcrh\build\training_project_1\practice_wh\cp\tools"
function cdwin(){
if [[ $1 = "--help" ]] || [[ $1 = "-h" ]] || [[ ! $1 ]]; then
echo "Usage: "
echo 'cdwin "Z:\bgcrh\build\training_project_1\practice_wh\cp\tools"'
echo "Input the win path --> $1"
elif [[ $1 =~ Z:.* ]]; then
line=$(echo $1 | sed -e 's#^Z:#/usrfiles#' -e 's#\\#/#g')
cd "$line"
else
echo -e "***Input a valid win path***\n"
echo "Usage: "
echo 'cdwin "Z:\bgcrh\build\training_project_1\practice_wh\cp\tools"'
fi
}
####################################### win2linux ##########################################
# win2linux "Z:\bgcrh\build\training_project_1\practice_wh\cp\tools"
function win2linux(){
if [[ $1 = "--help" ]] || [[ $1 = "-h" ]] || [[ ! $1 ]]; then
echo "Usage: "
echo 'win2linux "Z:\bgcrh\build\training_project_1\practice_wh\cp\tools"'
elif [[ $1 =~ Z:.* ]]; then
echo -e '\033[1;33mInput the win path\033[0m --> \c'
echo $1
line=$(echo $1 | sed -e 's#^Z:#/usrfiles#' -e 's#\\#/#g')
echo -e "\033[1;33mOutput the linux path\033[0m --> \c"
echo $line
else
echo -e "***Input a valid win path***\n"
echo "Usage: "
echo 'win2linux "Z:\bgcrh\build\training_project_1\practice_wh\cp\tools"'
fi
}
####################################### linux2win ##########################################
# linux2win "/usrfiles/bgcrh/build/training_project_1/practice_wh/cp/tools"
function linux2win(){
if [[ $1 = "--help" ]] || [[ $1 = "-h" ]] || [[ ! $1 ]]; then
echo "Usage: "
echo 'linux2win "/usrfiles/bgcrh/build/training_project_1/practice_wh/cp/tools"'
elif [[ $1 =~ /usrfiles.* ]]; then # match Z
echo -e '\033[1;33mInput the linux path\033[0m --> \c'
echo $1
line=$(echo $1 | sed -e 's#^/usrfiles#Z:#' -e 's#/#\\#g')
echo -e "\033[1;33mOutput the win path\033[0m --> \c"
echo $line
else
echo -e "***Input a valid win path***\n"
echo "Usage: "
echo 'linux2win "/usrfiles/bgcrh/build/training_project_1/practice_wh/cp/tools"'
fi
}
3. 配置環(huán)境(個人配置) :
step1: 上傳到mobaxterm
step2: 加載到Linux環(huán)境里面
- step2.1:如果是臨時使用,直接source腳本
kai.cao@beigenecorp.net@sas94p ~$ mkdir software
kai.cao@beigenecorp.net@sas94p ~$ source ~/path_convert.sh
- step2.2: step2.2: 如果想永久的使用寞秃,添加到配置文件~/.bashrc里面
vi ~/.bashrc 進入斟叼,使用vim 添加下面兩個打勾部分代碼,任意一個就行春寿。
4. 配置環(huán)境(服務器共享路徑) :
### add into ~/.bashrc
$ source /usrfiles/bgcrh/build/training_project_1/practice_wh/exercise/dev/pgm/learning/kai_cao/software/path_convert.sh
$ win2linux "Z:\bgcrh\build\training_project_1\practice_wh\exercise\dev\pgm\learning\kai_cao\software"
Input the win path --> Z:\bgcrh\build\training_project_1\practice_wh\exercise\dev\pgm\learning\kai_cao\software
Output the linux path --> /usrfiles/bgcrh/build/training_project_1/practice_wh/exercise/dev/pgm/learning/kai_cao/software
$ cdwin "Z:\bgcrh\build\training_project_1\practice_wh\exercise\dev\pgm\learning\kai_cao\software"
5. 三個函數(shù)使用的例子:
-
cdwin
用法:
-
linux2win
用法 :
-
win2linux
用法:
Tips:
- 注意shell腳本里面 不要出現(xiàn)tab;
- [[ ]] 字符串比較用雙中括號朗涩;數(shù)值比較用雙小括號;小數(shù)計算bc
- 字符串顏色標示 注意加上-e 參數(shù)绑改,但如果path里面存在路徑\t 或者\n寫法可能會識別為特殊符號谢床;
echo -e "\033[1;31mThis is bold red text\033[0m\n $path"
如果想把腳本直接加載到環(huán)境變量里面,需要用使用source test.sh而不是bash test.sh
exit功能: 退出當前sh或者退出文件執(zhí)行厘线。
歡迎大家評論交流??~