sh_config.sh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. #!/bin/bash
  2. # path去重
  3. export PATH=$(echo $PATH | tr ':' '\n' | cat -n | sort -k2,2 -k1,1n | uniq -f1 | sort -k1,1n | cut -f2- | tr '\n' ':')
  4. function targz() {
  5. tar -zcvf "${1}".tgz "$1"
  6. ll -h ${1}.tgz
  7. }
  8. function untar() {
  9. tar -zxvf "$1"
  10. }
  11. function sync_config_file() {
  12. mkdir -p ~/.config/my_config
  13. wget https://git.tianyunperfect.cn/tianyunperfect/sync/raw/master/sh_config.sh -O ~/.config/my_config/sh_config.sh && source ~/.config/my_config/sh_config.sh
  14. }
  15. # 快捷命令
  16. ## 未分类
  17. alias ll='ls -alh --time-style="+%Y-%m-%d %H:%I:%S"'
  18. #du -sh * 2>/dev/null | sort -hr
  19. alias du-sh='du -sh * 2>/dev/null | sort -hr'
  20. alias du-sh10='du -sh * 2>/dev/null | sort -hr | head -n 10'
  21. alias df-h='df -h | grep -v tmpfs | grep -v overlay' # 显示挂载的磁盘
  22. alias sourcevenv='source venv/bin/activate'
  23. alias weather='curl wttr.in/beijing'
  24. alias dust1='dust -d 1'
  25. function gittagpush() {
  26. if [ $# -ne 2 ]; then
  27. echo "需要两个参数: <变量1> <变量2>"
  28. return 1
  29. fi
  30. git tag -a $1 -m "$2"
  31. git push origin $1
  32. }
  33. alias gittags='git --no-pager tag -l -n'
  34. function reloadrc() {
  35. if [ -n "$ZSH_VERSION" ]; then
  36. echo "正在使用zsh"
  37. source ~/.zshrc
  38. elif [ -n "$BASH_VERSION" ]; then
  39. echo "正在使用bash"
  40. source ~/.bashrc
  41. else
  42. echo "无法确定当前 Shell 类型"
  43. fi
  44. }
  45. function install_mcfly() {
  46. # 用户目录是否存在 mcfly 文件
  47. if [ ! -f ~/mcfly ]; then
  48. wget https://tianyunperfect1.oss-cn-beijing.aliyuncs.com/file/2024/12/mcfly-v0.9.2-x86_64-unknown-linux-musl.tar.gz
  49. tar -zxvf mcfly-v0.9.2-x86_64-unknown-linux-musl.tar.gz
  50. rm -rf mcfly-v0.9.2-x86_64-unknown-linux-musl.tar.gz
  51. if [ -n "$ZSH_VERSION" ]; then
  52. echo "正在使用zsh"
  53. echo 'eval "$(mcfly init zsh)"' >>~/.zshrc
  54. elif [ -n "$BASH_VERSION" ]; then
  55. echo "正在使用bash"
  56. echo 'eval "$(mcfly init bash)"' >>~/.bashrc
  57. else
  58. echo "无法确定当前 Shell 类型"
  59. fi
  60. fi
  61. }
  62. # 安装监控用户命令
  63. function install_monitor_user_command() {
  64. if [ -n "$ZSH_VERSION" ]; then
  65. curl -SL https://git.tianyunperfect.cn/tianyunperfect/note_pub/raw/master/install_monitor_user_command_zsh.sh | sh
  66. elif [ -n "$BASH_VERSION" ]; then
  67. curl -SL https://git.tianyunperfect.cn/tianyunperfect/note_pub/raw/master/install_monitor_user_command_bash.sh | sh
  68. else
  69. echo "无法确定当前 Shell 类型"
  70. fi
  71. }
  72. function install_vimrc() {
  73. wget -O ~/.vimrc https://git.tianyunperfect.cn/tianyunperfect/note_pub/raw/master/.vimrc
  74. }
  75. function k8s-get-all-namespace() {
  76. kubectl get namespace
  77. }
  78. ## k8s
  79. function k8s-get-default-namespace() {
  80. kubectl config view | grep namespace
  81. }
  82. function k8s-set-default-namespace() {
  83. kubectl config set-context --current --namespace="$1"
  84. }
  85. function k8s-getpod() {
  86. k8s-get-default-namespace
  87. # 如果有参数,则过滤
  88. if [ ! -z "$1" ]; then
  89. kubectl get pods -o wide | grep "$1"
  90. else
  91. kubectl get pods -o wide
  92. fi
  93. }
  94. alias k8s-descpods='kubectl describe pods'
  95. function k8s-getlog() {
  96. k8s-get-default-namespace
  97. podName=$(kubectl get pods -o wide | grep "${1}" | awk '{print $1}')
  98. kubectl logs --tail=300 -f "${podName}"
  99. }
  100. function k8s-getlogbase() {
  101. k8s-get-default-namespace
  102. podName=$(kubectl get pods -o wide | grep "${1}" | awk '{print $1}')
  103. kubectl logs -c base-jre --tail=300 -f "${podName}"
  104. }
  105. function k8s-delpod() {
  106. kubectl delete pod "$1" --force
  107. }
  108. function k8s-inpod() {
  109. k8s-get-default-namespace
  110. podName=$(kubectl get pods -o wide | grep "${1}" | awk '{print $1}')
  111. kubectl exec -it "${podName}" -- /bin/bash
  112. }
  113. alias k8s-topnodes='kubectl top node'
  114. alias k8s-toppods='kubectl top pod'
  115. alias k8s-getsvc='kubectl get svc'
  116. alias k8s-descsvc='kubectl describe svc'
  117. alias k8s-deldeployment='kubectl delete deployment'
  118. alias k8s-getdeployment='kubectl get deployment'
  119. alias k8s-descdeployment='kubectl describe deployment'
  120. alias k8s-getdeploymentgrep='kubectl get deployment | grep'
  121. alias k8s-getingress='kubectl get ingress'
  122. alias k8s-descingress='kubectl describe ingress'
  123. alias k8s-getingressgrep='kubectl get ingress | grep'
  124. ## docker
  125. alias docker-rm-all-image='docker rmi $(docker images -q)'
  126. alias docker-rm-all-docker='docker rm $(docker ps -a -q)'
  127. alias docker-show-image-prune='docker image ls -f dangling=true'
  128. alias docker-image-prune='docker image prune -a'
  129. alias dkstats='docker stats'
  130. alias dkps='docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"'
  131. alias dkpsa='docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"'
  132. alias dkrm='docker rm -f'
  133. alias dkrmi='docker rmi -f'
  134. alias dkimage='docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}"'
  135. alias dkip='docker inspect --format="{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" $(docker ps -aq)'
  136. function dkin() {
  137. docker exec -it "$1" bash
  138. }
  139. function dkinsh() {
  140. docker exec -it "$1" sh
  141. }
  142. function dklog() {
  143. docker logs -n 300 -f "$1"
  144. }
  145. function dkinspect() {
  146. docker inspect "$1"
  147. }
  148. alias dkstart_all_exit='docker start $(docker ps -a -q -f status=exited)'
  149. ## go
  150. # 定义 gobuildwinexe 函数
  151. function gobuildwinexe() {
  152. if [ $# -ne 2 ]; then
  153. echo "用法: gobuildwinexe <源文件> <输出文件名>"
  154. return 1
  155. fi
  156. GOOS=windows GOARCH=amd64 go build -o "${2}.exe" "$1.go"
  157. }
  158. function gobuildlinuxexe() {
  159. if [ $# -ne 2 ]; then
  160. echo "用法: gobuildlinuxexe <源文件> <输出文件名>"
  161. return 1
  162. fi
  163. GOOS=linux GOARCH=amd64 go build -o "$2" "$1.go"
  164. }
  165. function gobuildmacexe() {
  166. if [ $# -ne 2 ]; then
  167. echo "用法: gobuildmacexe <源文件> <输出文件名>"
  168. return 1
  169. fi
  170. GOOS=darwin GOARCH=amd64 go build -o "$2" "$1.go"
  171. }
  172. ## version
  173. alias pythonversion='python --version'
  174. alias javaversion='java -version'
  175. alias nodeversion='node -v'
  176. alias centosversion='cat /etc/os-release'
  177. alias ubuntuversion='cat /etc/lsb-release'
  178. alias goversion='go version'
  179. ## ip查看
  180. alias ipremote='curl cip.cc'
  181. function iplocal() {
  182. # 如果是mac 过滤 en0,如果是linux 过滤 eth0
  183. if [[ "$OSTYPE" == "darwin"* ]]; then
  184. ifconfig en0 | grep inet | grep -v inet6
  185. else
  186. ifconfig eth0 | grep inet | grep -v inet6
  187. fi
  188. }
  189. ## 进程相关
  190. alias psgrep='ps aux | grep -v grep | grep'
  191. # 根据pod获取pid,示例: ss -tunlp | grep :8080
  192. function getpid-bypod() {
  193. if [ -z "$1" ]; then
  194. echo "请提供端口号作为参数"
  195. return 1
  196. fi
  197. ss -tunlp | grep ":$1"
  198. }
  199. function killport() {
  200. if [ -z "$1" ]; then
  201. echo "需要一个端口号作为参数"
  202. return 1
  203. fi
  204. kill -9 $(lsof -t -i:"$1")
  205. }
  206. function ps-mem-top6() {
  207. ps aux --sort=-%mem | awk '{print $2, $11, $4, $6/1024 "MB"}' | head -n 6
  208. }
  209. # node
  210. alias npminstall='npm install -g --registry=https://registry.npmmirror.com '
  211. # python
  212. # 定义 pipinstall 函数
  213. function pipinstall() {
  214. if [ -z "$1" ]; then
  215. echo "需要一个包名作为参数"
  216. return 1
  217. fi
  218. pip install "$1" -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
  219. }
  220. # 文件查找相关
  221. function findByName() {
  222. local pattern=$1
  223. find . -name "*$pattern*"
  224. }
  225. function findByContent() {
  226. local pattern=$1
  227. find . -type f -exec grep -l "$pattern" {} +
  228. }
  229. # 备份
  230. function tar_back() {
  231. time=$(date "+%Y-%m-%d_%H_%M_%S")
  232. tar -zcvf "${1}".${time}.tgz "$1"
  233. ll -h ${1}.${time}.tgz
  234. }
  235. pid-cmd-centos() {
  236. sudo ls -l /proc/$1/cwd | awk '{print $11}'
  237. sudo cat /proc/$1/cmdline
  238. echo ''
  239. }
  240. pid-cmd-ubuntu() {
  241. ls -l /proc/"$1"/cwd | awk '{print $11}'
  242. tr '\0' ' ' </proc/"$1"/cmdline
  243. echo ""
  244. }
  245. function install-arthas() {
  246. # 判断是否存在 ~/arthas-boot.jar,如果不存在则下载
  247. if [ ! -f ~/arthas-boot.jar ]; then
  248. (cd ~ && curl -O https://arthas.aliyun.com/arthas-boot.jar)
  249. fi
  250. # 判断是否在k8s里面
  251. if [[ ! -z "$KUBERNETES_SERVICE_HOST" && ! -z "$KUBERNETES_SERVICE_PORT" ]]; then
  252. echo "Running in Kubernetes environment"
  253. java -jar ~/arthas-boot.jar 1 --options json-format=true
  254. else
  255. echo "Not running in Kubernetes environment"
  256. java -jar ~/arthas-boot.jar --options json-format=true
  257. fi
  258. }
  259. alias cparthas='echo-arthas-install | pbcopy'
  260. function curl-form() {
  261. curl -d "$2" "$1"
  262. }
  263. function watch-cpudiskmemory() {
  264. watch -n 1 '
  265. echo "===CPU占用摘要:"
  266. top -bn 1 -i -c | grep Cpu
  267. echo "
  268. ===内存占用摘要:"
  269. free -h
  270. echo "
  271. ===磁盘占用摘要:"
  272. df -h /'
  273. }
  274. #123
  275. function vimrc() {
  276. if [[ "$SHELL" =~ "zsh" ]]; then
  277. code ~/.zshrc
  278. else
  279. code ~/.bashrc
  280. fi
  281. }
  282. merge_video_audio() {
  283. if [ "$#" -ne 3 ]; then
  284. echo "错误:参数数量不正确!"
  285. echo "用法:merge_video_audio video.mp4 audio.mp3 output.mp4"
  286. return 1
  287. fi
  288. ffmpeg -i "$1" -i "$2" -c:v copy -c:a copy "$3"
  289. }
  290. rm-all() {
  291. find . -name $1 | xargs -I {} rm -rf {}
  292. }
  293. ### mysql
  294. function echo-mysql-tables() {
  295. echo 'SELECT table_name, table_comment FROM information_schema.tables WHERE table_schema = DATABASE();'
  296. }
  297. function echo-mysql-table-size() {
  298. echo 'select table_name,concat(round(sum(DATA_LENGTH/1024/1024),2),"MB") as data from information_schema.TABLES where table_schema =(SELECT DATABASE()) group by table_name;'
  299. }
  300. function echo-mysql-db-size() {
  301. echo 'select table_schema,concat(round(sum(DATA_LENGTH/1024/1024),2),"MB") as data from information_schema.TABLES where table_schema not in ("information_schema","mysql","performance_schema","sys") group by table_schema;'
  302. }
  303. function echo-npm() {
  304. echo '
  305. npm i webpack # 在module文件中安装
  306. npm i webpack -S # --save 常用,添加到package
  307. npm i webpack -D # --save-dev
  308. npm i webpack -g --registry=https://registry.npmmirror.com # 全局
  309. npm un webpack -g
  310. yarn add react
  311. yarn remove react
  312. yarn global add umi
  313. '
  314. }
  315. concat_params() {
  316. # 获取传入的参数个数
  317. num_params=$#
  318. # 判断参数个数是否大于等于1
  319. if [ $num_params -ge 1 ]; then
  320. # 使用循环遍历参数,并拼接在一起
  321. concat_str="$1"
  322. for ((i = 2; i <= $num_params; i++)); do
  323. concat_str+="%20${!i}"
  324. done
  325. # 打印拼接后的字符串
  326. echo "$concat_str"
  327. else
  328. echo "没有传入参数"
  329. fi
  330. }
  331. function url_encode() {
  332. echo "$1" | sed 's/ /%20/g'
  333. }
  334. function bing() {
  335. ## 如果是windows系统,则使用start,否则使用open,后面的参数可以有多个空格
  336. if [[ "$OSTYPE" == "msys"* ]]; then
  337. search_query=$(concat_params "$@")
  338. start "https://www.bing.com/search?q=$search_query"
  339. else
  340. search_query=""
  341. for query in "$@"; do
  342. search_query+=" $query"
  343. done
  344. search_query=$(url_encode "$search_query")
  345. open "https://www.bing.com/search?q=${search_query}"
  346. fi
  347. }
  348. function mvn_clean_package() {
  349. mvn clean package -Dmaven.test.skip=true
  350. }
  351. ## ska快捷
  352. function ska_push_dev() {
  353. skaffold run -p dev --tail
  354. }
  355. function mvn_push_dev() {
  356. mvn_clean_package
  357. ska_push_dev
  358. }
  359. function mvn_push_test() {
  360. mvn_clean_package
  361. skaffold build -p test -n aimptest
  362. }
  363. function mvn_clean_deploy() {
  364. mvn clean deploy -Dmaven.test.skip=true
  365. }
  366. function show_all_local_ip() {
  367. arp -a
  368. }
  369. alias nodels='fnm ls'
  370. alias nodeuse='fnm use'
  371. alias nodedefault='fnm default'
  372. alias nodeinstall='fnm install'
  373. alias nodelsremote='fnm ls-remote'
  374. alias nodeuninstall='fnm uninstall'
  375. alias catnodeversion='node --version > .node-version'
  376. alias yarnglobaladd='yarn global add'
  377. alias yarnglobalremove='yarn global remove'
  378. alias yarnadd='yarn add'
  379. alias yarnremove='yarn remove'
  380. alias yarninstall='yarn install'
  381. function echosync() {
  382. echo '
  383. source <(curl -s https://git.tianyunperfect.cn/tianyunperfect/sync/raw/master/sh_config.sh)
  384. '
  385. }
  386. alias settitle='wezterm cli set-tab-title '
  387. function btm_linux() {
  388. if [ ! -f "$HOME/btm" ]; then
  389. wget https://tianyunperfect1.oss-cn-beijing.aliyuncs.com/20/2024/btm/btm -O ~/btm
  390. chmod +x ~/btm
  391. fi
  392. ~/btm --process_memory_as_value
  393. }
  394. # watch 优化
  395. alias watch_alias='watch '
  396. # 函数,每隔2秒执行一次参数命令
  397. function watch_func() {
  398. while true; do
  399. clear
  400. "$@"
  401. sleep 2
  402. done
  403. }
  404. alias gpu_nvidia='nvidia-smi'
  405. alias gpu_amd='rocm-smi --showpids -u -P'
  406. alias iptables_show_all='iptables -t nat -L -n'
  407. # docker-compose up ,如果有参数,则使用-f 文件
  408. function docker_compose_up() {
  409. if [ $# -eq 0 ]; then
  410. docker-compose up -d
  411. return
  412. fi
  413. docker-compose -f "$1" up -d
  414. }
  415. # 同理 docker-compose down
  416. function docker_compose_down() {
  417. if [ $# -eq 0 ]; then
  418. docker-compose down
  419. return
  420. fi
  421. docker-compose -f "$1" down
  422. }
  423. # Ubuntu/Debian: sudo apt-get install moreutils
  424. # CentOS/RHEL: sudo yum install moreutils
  425. # macOS: brew install moreutils
  426. # 用法 tail -f abc.log | pt
  427. alias pt="ts '[%Y-%m-%d %H:%M:%S]'"
  428. alias myrsync='rsync -avhP --delete --no-owner --no-group'
  429. # 函数:生成指定大小的文件 generate_file 2 M "logfile.log"
  430. generate_file() {
  431. local size=$1
  432. local unit=$2
  433. local filename=${3:-"generated_file.bin"} # 默认文件名为generated_file.bin
  434. # 检查单位是否正确
  435. case $unit in
  436. "B" | "b")
  437. bs=1
  438. ;;
  439. "K" | "k")
  440. bs=1K
  441. ;;
  442. "M" | "m")
  443. bs=1M
  444. ;;
  445. "G" | "g")
  446. bs=1G
  447. ;;
  448. *)
  449. echo "错误:不支持的单位。请使用 B, K, M, 或 G。"
  450. return 1
  451. ;;
  452. esac
  453. # 使用dd命令生成文件
  454. dd if=/dev/urandom of="$filename" bs=$bs count=$size status=none
  455. }
  456. # 函数:显示当前网速
  457. function band_linux() {
  458. # 检查 bandwhich 是否已经下载
  459. if [ ! -f "$HOME/bandwhich" ]; then
  460. cd ~
  461. # 检查 glibc 版本
  462. GLIBC_VERSION=$(ldd --version | head -n 1 | awk '{print $NF}')
  463. REQUIRED_VERSION="2.29"
  464. # 比较版本号
  465. if printf '%s\n%s' "$REQUIRED_VERSION" "$GLIBC_VERSION" | sort -V | head -n 1 | grep -qx "$REQUIRED_VERSION"; then
  466. echo "Glibc version $GLIBC_VERSION meets requirements, downloading GNU version..."
  467. wget https://tianyunperfect1.oss-cn-beijing.aliyuncs.com/file/2024/11/bandwhich-v0.23.1-x86_64-unknown-linux-gnu.tar.gz
  468. tar -zxvf bandwhich-v0.23.1-x86_64-unknown-linux-gnu.tar.gz
  469. rm -rf {bandwhich-v0.23.1-x86_64-unknown-linux-gnu.tar.gz,assets}
  470. else
  471. echo "Glibc version $GLIBC_VERSION does not meet requirements, downloading musl version..."
  472. wget https://tianyunperfect1.oss-cn-beijing.aliyuncs.com/file/2024/11/bandwhich-v0.23.1-x86_64-unknown-linux-musl.tar.gz
  473. tar -zxvf bandwhich-v0.23.1-x86_64-unknown-linux-musl.tar.gz
  474. rm -rf {bandwhich-v0.23.1-x86_64-unknown-linux-musl.tar.gz,assets}
  475. fi
  476. cd -
  477. fi
  478. # 运行 bandwhich
  479. sudo ~/bandwhich
  480. }
  481. alias reload_docker_config='sudo systemctl daemon-reload && sudo systemctl restart docker'
  482. # 函数 vim 替代终端
  483. function vim_micro_linux() {
  484. if [ ! -f "$HOME/micro" ]; then
  485. cd ~
  486. wget https://tianyunperfect1.oss-cn-beijing.aliyuncs.com/file/2024/11/micro-2.0.14-linux64-static.tgz
  487. tar -zxvf micro-2.0.14-linux64-static.tgz
  488. mv micro-2.0.14/micro ~
  489. chmod +x ~/micro
  490. rm -rf {micro-2.0.14,micro-2.0.14-linux64-static.tgz}
  491. echo "vim_micro_linux 安装完成, 开始写入配置文件"
  492. mkdir -p ~/.config/micro
  493. cat <<EOF >~/.config/micro/bindings.json
  494. {
  495. "Alt-q": "Quit"
  496. }
  497. EOF
  498. cd -
  499. fi
  500. ~/micro "$@"
  501. }
  502. function install_zellij() {
  503. # 用户目录是否存在 zellij 文件
  504. if [ ! -f ~/zellij ]; then
  505. wget https://fs.n1.tianyunperfect.cn:10086/2025/zellij-x86_64-unknown-linux-musl.tar.gz
  506. tar -zxvf zellij-x86_64-unknown-linux-musl.tar.gz
  507. rm -rf zellij-x86_64-unknown-linux-musl.tar.gz
  508. fi
  509. }
  510. # 磁盘读写速度测试
  511. function my_disk_speed_test() {
  512. # 首先同步缓存,确保测试准确
  513. sync
  514. echo 3 >/proc/sys/vm/drop_caches
  515. echo "测试写入"
  516. # 测试写入速度 (写入1GB数据)
  517. dd if=/dev/zero of=./testfile bs=1M count=1024 conv=fdatasync
  518. echo "测试读取"
  519. # 测试读取速度
  520. dd if=./testfile of=/dev/null bs=1M count=1024
  521. # 测试完成后删除测试文件
  522. rm ./testfile
  523. }
  524. # 获取未使用的端口
  525. function my_unused_port() {
  526. for port in {8000..65535}; do
  527. if ! ss -tuln | grep -q ":$port "; then
  528. echo $port
  529. break
  530. fi
  531. done
  532. }
  533. # 查看当前挂载目录文件大小,不查看其他挂载目录
  534. function my_mounted_dir_size() {
  535. # 默认当前目录
  536. if [ -z "$1" ]; then
  537. dir="."
  538. else
  539. dir="$1"
  540. fi
  541. du -hx --max-depth=1 ${dir} | sort -hr
  542. }
  543. # 设置终端标题
  544. set_title() {
  545. printf "\033]2;%s\007" "$1"
  546. }
  547. ## ffmpeg 相关
  548. # 提起音频,入参是文件名,默认转出同名的mp3,示例:ffmpeg -i input.mp4 -vn -acodec libmp3lame -q:a 0 output.mp3
  549. function ffmpeg_extract_audio() {
  550. if [ -z "$1" ]; then
  551. echo "Usage: ffmpeg_extract_audio <input_file>"
  552. return 1
  553. fi
  554. # 获取文件名
  555. filename=$(basename "$1")
  556. # 获取文件扩展名
  557. extension="${filename##*.}"
  558. # 获取文件名(不包含扩展名)
  559. filename_without_extension="${filename%.*}"
  560. # 默认输出文件名
  561. output_file="${filename_without_extension}.mp3"
  562. # 提取音频 ffmpeg -i input.mp4 -vn -acodec libmp3lame -q:a 0 output.mp3
  563. ffmpeg -i "$1" -vn -acodec libmp3lame -q:a 0 "$output_file"
  564. }
  565. # 合并视频和音频
  566. ffmpeg_merge_video_audio() {
  567. # 检查参数数量是否正确
  568. if [ "$#" -ne 3 ]; then
  569. echo "用法: merge_video_audio <视频文件> <音频文件> <输出文件>"
  570. return 1
  571. fi
  572. # 获取参数
  573. video_path="$1"
  574. audio_path="$2"
  575. output_path="$3"
  576. # 检查输入文件是否存在
  577. if [ ! -f "$video_path" ]; then
  578. echo "错误: 视频文件 '$video_path' 不存在"
  579. return 1
  580. fi
  581. if [ ! -f "$audio_path" ]; then
  582. echo "错误: 音频文件 '$audio_path' 不存在"
  583. return 1
  584. fi
  585. # 执行 FFmpeg 命令
  586. ffmpeg -i "$video_path" -i "$audio_path" -c:v copy -c:a copy "$output_path"
  587. # 检查命令是否成功执行
  588. if [ $? -eq 0 ]; then
  589. echo "合并完成: 输出文件为 '$output_path'"
  590. else
  591. echo "错误: 合并失败"
  592. return 1
  593. fi
  594. }
  595. ffmpeg_convert_to_mp3() {
  596. # 检查参数数量是否正确
  597. if [ "$#" -ne 2 ]; then
  598. echo "用法: convert_to_mp3 <输入音频文件> <输出MP3文件>"
  599. return 1
  600. fi
  601. # 获取参数
  602. input_file="$1"
  603. output_file="$2"
  604. # 检查输入文件是否存在
  605. if [ ! -f "$input_file" ]; then
  606. echo "错误: 输入文件 '$input_file' 不存在"
  607. return 1
  608. fi
  609. # 执行 FFmpeg 命令
  610. ffmpeg -i "$input_file" -vn -acodec libmp3lame -q:a 2 "$output_file"
  611. # 检查命令是否成功执行
  612. if [ $? -eq 0 ]; then
  613. echo "转换完成: 输出文件为 '$output_file'"
  614. else
  615. echo "错误: 转换失败"
  616. return 1
  617. fi
  618. }