要在其他目录中运行命令,可以使用以下Bash脚本:
#!/bin/bash
# 设置要运行命令的目录
target_dir="/path/to/target/directory"
# 进入目标目录
cd "$target_dir" || exit
# 运行命令
command_to_run="your_command_here"
$command_to_run
在脚本中,首先将target_dir
变量设置为要运行命令的目录的路径。然后,使用cd
命令进入目标目录。如果无法进入目录(例如,目录不存在),则使用exit
命令退出脚本。
最后,将command_to_run
变量设置为要在目标目录中运行的命令,并使用$command_to_run
来运行命令。
确保将/path/to/target/directory
替换为实际的目标目录路径,并将your_command_here
替换为要在目标目录中运行的实际命令。
这样,脚本将进入目标目录并运行指定的命令,而不考虑当前所在的目录层级。