Simulate User Input To Call Script Multiple Times With Different Parameters

江枫思渺然 提交于 2019-12-24 02:25:31

问题


I have to use a provided script that takes user input while the script is running instead of parameters. I can't get around this.

An example of script would be:

#!/bin/bash

echo "param one"
read one
doSomething

echo "param two"
read two
doSomething

echo "param three"
read three
doSomething

echo "param four"
read four
doSomething

echo "param five"
read five
doSomething

I would like a way to be able to call this script and provide parameterized input, something like:

./scriptNameWrapper.ksh 1 22 333 4444 55555

I've tried googling, and either I'm not asking the question correctly, or more likely I can't see the wood for the trees.

I've tried this, which doesn't work:

#!/bin/bash

./scriptName.ksh
<<$1
<<$2
<<$3
<<$4
<<$5

I'm clearly not a *nix expert, but I'm sure I've seen this done before, I just can't find any examples out there. This is beginning to get frustrating, and any help would be greatly appreciated.


回答1:


What you've probably seen, based on your attempt, is called a here-document.

It should look like this:

#!/bin/bash

./scriptName.ksh <<-END_PARAMS
    $1
    $2
    $3
    $4
    $5
END_PARAMS



回答2:


Call your script like this:

echo -e "Param1\nParam2\nParam3" | ./scriptName.ksh

The \n sequence between each parameter emulates the enter key being pressed.




回答3:


Put your paramaters in a file one per line then run

./scriptName.ksh <filename


来源:https://stackoverflow.com/questions/3213127/simulate-user-input-to-call-script-multiple-times-with-different-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!