sh脚本模块笔记 2024-06-23 15:20 python, 笔记, 运维, linux, 开发语言 60人 已看 sh脚本模块笔记 1. 如果不存在,则创建指定目录 2. 检测python启动指令 记录一些sh脚本中用到过的模块 1. 如果不存在,则创建指定目录 这个脚本首先会检查指定的目录是否存在,如果存在则会打印错误消息并退出,否则会创建该目录(包括任何必要的父目录)。 #!/bin/sh dir="/path/to/your/directory" if [ -d "$dir" ]; then echo "错误:目录 '$dir' 已经存在。" exit 1 else mkdir -p "$dir" echo "目录 '$dir' 已经被创建。" fi 2. 检测python启动指令 # 检查python3是否存在 if command -v python3 &>/dev/null; then python_executable="python3" # 检查python是否存在 elif command -v python &>/dev/null; then python_executable="python" else echo "Python is not installed. Please install it first." exit 1 fi