如何通过用户输入为脚本中的命令“填空”?

时间:2018-06-30 05:45:06

标签: linux shell automation

我正在尝试构建一个脚本,该脚本要求运行脚本的用户提供一个时钟编号和一个DC编号,我打算为此填写Xs

/u/applic/tna/shell/tc_software_update.sh tmcxx.s0xxxx.us REFURBISHED

但是,我对如何让用户输入的内容填充脚本中该命令的那些X感到困惑。该脚本尚处于最早阶段,因此目前非常粗糙。感谢您的回应。这是我正在使用的脚本框架:

#!/bin/bash

#This server is intended to speed up the process to setup timeclocks from DC tickets

#Defines time clock numbers

timeclocks="01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35"

#Defines DC number

echo “What is the DC number?”

read dc

#Defines TMC number

echo "What is the Time Clock number?"

read number

if $number == $timeclocks && $dc == ???; then
/u/applic/tna/shell/tc_software_update.sh tmcxx.s0xxxx.us REFURBISHED

2 个答案:

答案 0 :(得分:1)

您是说调用$ /u/applic/tna/shell/tc_software_update.sh tmc${number}.s0${dc}.us REFURBISHED吗?

请考虑以下代码段:

[test.sh]
read x
read y
echo "x=${x}, y=${y}"

$ sh test.sh 
5
4
x=5, y=4

此外,您可以使用命令行参数($ 1,$ 2等)代替用户输入。

答案 1 :(得分:0)

在您的脚本上对此建模:

timeclocks=( {1..35} )
printf '%s' "DC number: "; read dc
printf '%s' "Time Clock number: "; read tmc
tmc=$( printf '%02d' "$tmc" )
dc=$( printf '%04d' "$dc" )
tmc_valid=$( for t in ${timeclocks[@]}; do \
    [[ $tmc -eq $t ]] && echo true && break; \
    done )
[[ "$tmc_valid" = "true" && "$dc" = "???" ]] && \
/u/applic/tna/shell/tc_software_update.sh tmc${tmc}.s0${dc}.us REFURBISHED