目的:
一个路径转换的函数:方便大家在aws 里面进行linux和windows 路径的转换;
(使用上一定注意加上引号cdwin "Z:\bgcrh\build\training_project_1\practice_wh\cp\tools"
)
参考资料:
https://www.coder.work/article/176623 转换函数
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 函数定义
字符串判断用[[ ]] || [[]] ; 数字判断使用(( ))
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 保存函数
测试
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. 配置环境(个人配置) :
step1: 上传到mobaxterm
step2: 加载到Linux环境里面
- 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. 配置环境(服务器共享路径) :
### 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. 三个函数使用的例子:
-
cdwin
用法:
-
linux2win
用法 :
-
win2linux
用法:
Tips:
- 注意shell脚本里面 不要出现tab;
- [[ ]] 字符串比较用双中括号;数值比较用双小括号;小数计算bc
- 字符串颜色标示 注意加上-e 参数,但如果path里面存在路径\t 或者\n写法可能会识别为特殊符号;
echo -e "\033[1;31mThis is bold red text\033[0m\n $path"
如果想把脚本直接加载到环境变量里面,需要用使用source test.sh而不是bash test.sh
exit功能: 退出当前sh或者退出文件执行。
欢迎大家评论交流😊~