我是 bash 新手。需要针对以下问题提出建议。
所以我想以这种方式执行脚本
./myscript --bootstrap bootstrap.exe --vmmount ./vmmount --image /dev/sdb2 --target-exe installer.exe [--internal-exe] param1 param2 param3 ...
Run Code Online (Sandbox Code Playgroud)
我做过的参数解析器:
VMMOUNT=""
BOOTSTRAP=""
IMAGE_FILE=""
TARGET_EXE=""
INTERNAL_EXE=""
while : ; do
if [ "$1" = "--vmmount" ] ; then
[ -n "${VMMOUNT}" ] && usage
VMMOUNT="$2"
shift
shift
elif [ "$1" = "--bootstrap" ] ; then
[ -n "${BOOTSTRAP}" ] && usage
BOOTSTRAP="$2"
shift
shift
elif [ "$1" = "--image" ] ; then
[ -n "${IMAGE_FILE}" ] && usage
IMAGE_FILE="$2"
shift
shift
elif …Run Code Online (Sandbox Code Playgroud) 我正在尝试想出一种方法脚本来在 bash 中传递一个无声标志,以便所有输出都将被定向到/dev/null它是否存在,如果它不存在则定向到屏幕。
我的脚本的 MWE 将是:
#!/bin/bash
# Check if silent flag is on.
if [ $2 = "-s" ]; then
echo "Silent mode."
# Non-working line.
out_var = "to screen"
else
echo $1
# Non-working line.
out_var = "/dev/null"
fi
command1 > out_var
command2 > out_var
echo "End."
Run Code Online (Sandbox Code Playgroud)
我用两个变量调用脚本,第一个是不相关的,第二个 ( $2) 是实际的静默标志( -s):
./myscript.sh first_variable -s
Run Code Online (Sandbox Code Playgroud)
显然,out_var行不工作,但他们给我想要的东西的想法:以直销的方式输出command1,并command2在屏幕或来/dev/null根据-s存在与否。
我怎么能这样做?
我是Unix shell脚本的新手,想要写一些小脚本的帮助.
我为我的脚本定义了以下概要:
install.sh [-h|-a path|[-k path][-f path][-d path][-e path]]
Run Code Online (Sandbox Code Playgroud)
即,用户可以请求一些help (-h),将所有内容安装到指定的位置(-a path),或者将一个或多个组件(-k, -f, -d -e)安装到适当的路径.如果没有参数,则应显示帮助.
提前致谢.
知道我能够运行echo或mv扩展这样的模式:echo {0..9}{A..Z}.我很想知道是否有办法执行相同操作但运行命令?
docker-compose {stop,rm,up -d}
Run Code Online (Sandbox Code Playgroud)
上面的例子不起作用,但有一些方法可以实现(分别运行stop,rm和up)?
我只是编写了一个用于管理多个并行ssh命令的bash脚本.为了解析参数,我使用这段代码:
#!/bin/bash
# replace long arguments
for arg in "$@"; do
case "$arg" in
--help) args="${args}-h ";;
--host|-hS) args="${args}-s ";;
--cmd) args="${args}-c ";;
*) [[ "${arg:0:1}" == "-" ]] && delim='' || delim="\""
args="${args}${delim}${arg}${delim} ";;
esac
done
echo "args before eval : $args"
eval set -- $args
echo "args after eval : $args"
while getopts "hs:c:" OPTION; do
echo "optarg : $OPTARG"
case $OPTION in
h) usage; exit 0;;
s) servers_array+=("$OPTARG");;
c) cmd="$OPTARG";;
esac
done
Run Code Online (Sandbox Code Playgroud)
所以我可以使用例如-s, - host或-hS来获得相同的结果.除了一件事,一切都很好.
如果我在参数中放入一个变量,它将被评估.
我想完全离线下载一些网页,以便稍后查看,只是单个页面,而不是整个网站。但是我在网上找不到可用的解决方案。
\n\n我看到一些建议使用该wget -E -H -k -K -p url命令的答案。但这根本没有解决问题。因为它从其他网站下载很多很多页面。
我\xe2\x80\x99ve也尝试了cURL命令,但该页面没有完全离线保存,当用Firefox打开它时,它不断加载其他地址,因此没有互联网连接就无法查看。
\n\n我只是希望单个页面完全离线,这样即使原始网站关闭我也可以查看它。将示例页面 url 设为:
\n\nhttp://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash\nRun Code Online (Sandbox Code Playgroud)\n 目前,我有一些 flag -x,以及其他一些:
while getopts abcx opt; do
case $opt in
a) a=true; ;;
b) b=true; ;;
c) c=true; ;;
x) x=true; ;;
esac
done
Run Code Online (Sandbox Code Playgroud)
但是,我想让它添加一个x会增加标志的级别。我已经看到这个vfor verbose 额外的 v 增加了冗长。如何在我的 Bash 脚本中检测到这一点?
我是 unix 脚本的新手。我正在尝试创建一个充当 unix 命令的脚本,例如“ls -l”、“ls -la”
myscript -x ----> 做第 1 件事 myscript -xy ----> 做第 1 件事,做第 2 件事 myscript -yz ----> 做第 2 件事,做第 3 件事
那么,“-x”、“-xy”是 $0 吗?或者我们需要使用不同的变量来获得它?
谢谢
我需要能够备份并稍后使用(读取和打印以及作为参数传递给另一个命令)所有输入参数到 bash 程序,但无法弄清楚。这是我的尝试:
back_up_all_input_args.sh:
#!/usr/bin/env bash
all_args1="$@"
all_args2="$(printf "%q " "$@")"
# Simulate parsing the input args here
# - see: https://stackoverflow.com/a/14203146/4561887
shift # remove 1st arg
shift # remove 2nd arg
echo "$@"
echo "$all_args1"
echo "$all_args2"
# Do another program call with all input args here
# rg "$all_args1" # FAILS
# rg "$all_args2" # FAILS
Run Code Online (Sandbox Code Playgroud)
示例运行和输出:
Run Code Online (Sandbox Code Playgroud)$ ./backup_all_input_args.sh arg1 "arg 2" "arg 3" arg 3 arg1 arg 2 arg 3 arg1 arg\ 2 arg\ 3
我的第一个方法是用 来支持论点 …
我需要修改我的一个脚本。到目前为止,它有两个强制性参数,它们指出更新的版本和将应用此更新的数据库
./script.sh version db_name
Run Code Online (Sandbox Code Playgroud)
现在我想添加两个新的可选参数,实际上我应该将其称为开关。这些开关将其扩展为: 1. 在安装之前停止(或不停止)我的 Web 服务器 2. 还将一些新文件安装到文件系统 两者都返回 bool 值。所有细节都在脚本内。所以我期望类似的东西:
./script.sh version db_name -stopweb -copyfiles
Run Code Online (Sandbox Code Playgroud)
我发现getopts是合适的命令。问题是如何将参数(强制)和开关(可选)“连接”在一起。我真的不明白:(请你给我一些提示。
我编写了一个帮助菜单,以供参考shell脚本的使用 my_script.sh
echo $'\n\n'
echo $(printf '=%.0s' {1..100})
printf ' %.0s' {1..40}
echo "Welcome"
echo $(printf '=%.0s' {1..100})
echo $'\n'
arg=$1
echo "Input : $arg"
echo
if [[ arg -eq "-h" ]] || [[ arg -eq "-H" ]] || [[ arg -eq "-help" ]] || [[ arg -eq "-Help" ]] || [[ arg -eq "--h" ]] || [[ arg -eq "--H" ]] || [[ arg -eq "--help" ]] || [[ arg -eq "--Help" ]]; then
echo "Help menu requested...."
echo …Run Code Online (Sandbox Code Playgroud)