I have a requirement where ScriptA.sh has commands to ask for User's inputs and perform a set of actions. I want to automate this by creating another script which will read the questions asked from output of ScriptA.sh and provide the necessary values in runtime.
ScriptA.sh as follows :-
echo "Enter the CR Number"
read varnamecr
echo "CR Number is" $varnamecr
echo "Loading the config set. Choose Option From Below set
1.JAN
2.FEB
3.MAR"
read optionchoosen
echo "Option Choosen is :" $optionchoosen
echo "Will run the script/load configuration is this Ok ?[y/N]"
read userinput
echo "Proceed further, User has pressed ->"$userinput"<--Key"
How to write the second script to achieve this. Tried spawn and few other commands in the second script, but no luck. Please help me with this.
Since you're not specifying any shell in your tag, this is a possible, albeit crude, solution in ksh. It's using the coprocess capability of that shell (pretty sure it's not supported in bash although please don't quote me on that one)
#!/bin/ksh
./ScriptA.sh |&
while read -p Dummy; do
print $Dummy
case $Dummy in
"Enter the CR Number")print -p "CR123456"
;;
"3.MAR")print -p "3"
;;
"Will run the script"*)print -p "y"
;;
esac
done
The output gives :
Enter the CR Number
CR Number is CR123456
Loading the config set. Choose Option From Below set
1.JAN
2.FEB
3.MAR
Option Choosen is : 3
Will run the script/load configuration is this Ok ?[y/N]
Proceed further, User has pressed ->y<--Key
Will input remain same everytime? If so you can create wrapper of this script to provide required input.
cat wrapper
./ScriptA.sh <<!
123
2
y
!
Related
Using KornShell, I want to ask the user for an input, and use a default value if no input is given. Of course if he entered something it should use the entered value.
My script so far:
echo "Choose a script to start"
read input?"Start Script: "
The default value should be next to the question, like this:
Start script: [script1]
KornShell is something new for me, but I am interested in it and would like to get to know it.
You didn't mention what the default is, or how you want to manage it (eg, as a variable, as a static string).
There are many ways to handle the input; one simple example ...
The script (wanna_play):
$ cat wanna_play
#!/bin/ksh
dflt='spider'
echo "Choose a script to start"
read input?"Start Script: [${dflt}] "
[[ "${input}" = '' ]] && input=${dflt}
echo "Entered: ${input}"
And a couple sample runs:
$ wanna_play
Choose a script to start
Start Script: [spider]
Entered: spider
$ wanna_play
Choose a script to start
Start Script: [spider] freecell
Entered: freecell
I suggest you do some web searches for learning ksh ... lots of useful info on the internet; one good starter book: O'Reilly: Learning the Korn Shell
I tried to answer another SO question with a simple menu using the builtin select statement. The code displays names from /etc/passwd, and let you select a name by giving a number:
PS3="Enter a number: "
select name in $(cut -d: -f1 /etc/passwd) ; do
if [ -n "${name}" ]; then
break
fi
echo "Sorry, please enter a number as shown."
done
echo "Entry from passwd is: ${name}"
The works fine except for the very first time. When you give a correct answer the first time it will ask you to try again.
I tried to get a more detailed explanation of the first time, but I couldn't get a reproducable cook-book. When you copy-paste this code on your server, and give a correct answer you will probably have the same problem. When you repeat the command (from history or a new paste), the code shows now problem. I tried to get the problem again by logging out and logging in (sometimes it works) or rebooting.
I tried different ways to reproduce the problem in other situations (using different variable names, unsetting variables, using a slow list of values with select name in $(echo One; sleep 1; echo Two; sleep 2; echo Three; sleep 1); and opening a new shell.
I searched for other examples with select, but I can't find clues in other posts like https://stackoverflow.com/a/16750755/3220113 and https://askubuntu.com/a/1716.
I tried to fix my code with a sync and that seems to be a work-around:
PS3="Enter a number: "
select name in $(cut -d: -f1 /etc/passwd) ; do
# is sync needed here?
sync
if [ -n "${name}" ]; then
break
fi
echo "Sorry, please enter a number as shown."
done
echo "Entry from passwd is: ${name}"
I couldn't reproduce the error when I include the sync command. Is sync really a working patch and why do I need this here?
I do not need other ways to write a menu. I already found the graphical dialog Dialog from bash script and was looking for a simple replacement of my own over-complicated https://unix.stackexchange.com/a/115371/57293.
This problem only occurs when you type the commands interactively, not in a script. The reason is that the line you type after the select line is being used as the response to the prompt. Since if isn't in the menu, it reports an error. Then it doesn't execute the if command, because it was read as the response to the prompt.
It's not a problem in a script because the commands in the script are not used as standard input.
I'm new to Unix...I have a shell script that calls sqlplus. I have some variables that are defined within the code. However, I do not feel comfortable having the password displayed within the script. I would appreciate if someone could show me ways on how to hide my password.
One approach I know of is to omit the password and sqlplus will
prompt you for the password.
An approach that I will very much be interested in is a linux
command whose output can be passed into the password variable. That
way, I can replace easily replace "test" with some parameter.
Any other approach.
Thanks
#This is test.sh It executes sqlplus
#!/bin/sh
export user=TestUser
export password=test
# Other variables have been ommited
echo ----------------------------------------
echo Starting ...
echo ----------------------------------------
echo
sqlplus $user/$password
echo
echo ----------------------------------------
echo finish ...
echo ----------------------------------------
You can pipe the password to the sqlplus command:
echo ${password} | sqlplus ${user}
tl;dr: passwords on the command line are prone to exposure to hostile code and users. don't do it. you have better options.
the command line is accessible using $0 (the command itself) through ${!#} ($# is the number of arguments and ${!name} dereferences the value of $name, in this case $#).
you may simply provide the password as a positional argument (say, first, or $1), or use getopts(1), but the thing is passwords in the arguments array is a bad idea. Consider the case of ps auxww (displays full command lines of all processes, including those of other users).
prefer getting the password interactively (stdin) or from a configuration file. these solutions have different strengths and weaknesses, so choose according to the constraints of your situation. make sure the config file is not readable by unauthorized users if you go that way. it's not enough to make the file hard to find btw.
the interactive thing can be done with the shell builtin command read.
its description in the Shell Builtin Commands section in bash(1) includes
-s Silent mode. If input is coming from a terminal, characters are not echoed.
#!/usr/bin/env bash
INTERACTIVE=$([[ -t 0 ]] && echo yes)
if ! IFS= read -rs ${INTERACTIVE+-p 'Enter password: '} password; then
echo 'received ^D, quitting.'
exit 1
fi
echo password="'$password'"
read the bash manual for explanations of other constructs used in the snippet.
configuration files for shell scripts are extremely easy, just source ~/.mystuffrc in your script. the configuration file is a normal shell script, and if you limit yourself to setting variables there, it will be very simple.
for the description of source, again see Shell Builtin Commands.
I'm trying to automate running of a shell script that would take some user inputs at various points of its execution.
The basic logic that I've in my mind is copied below, but this is only for one input. I wanna run it recursively until the shell prompt is received after the original script completes its execution. I said recursively because, the question that prompts for an input and the input itself will be the same all the time.
#!/usr/bin/expect
spawn new.sh $1
expect "Please enter input:"
send "my_input"
Sharing any short-cut/simple method to achieve this will be highly appreciated.
You don't need expect to do this - read can read from a pipe as well as from user input, so you can pass the input through a pipe to your script. Example script:
#!/bin/bash
read -p "Please enter input: " input
echo "Input: $input"
Running the script prompts for input as normal, but if you pipe to it:
$ echo "Hello" | sh my_script.sh
Input: Hello
You said that your input is always the same - if so, then you can use yes (which just prints a given string over and over) to pass your script the input repeatedly:
yes "My input" | sh my_script.sh
This would run my_script.sh, any read commands within the script will read "My input".
I'm not sure if the title explain the real problem.
I have a shell script load_config.shusing bash shell. This script will load the configuration. Before starting to load the configuration, it prompt the user "Do you want to load all configurations? Y/N ?". If the user choose "N" it does a different job with many questions prompting.
Now the real problem is; I have added this script to run in Makefile as below. My idea is to load all the configuration when I say make install, but then the script would prompt the user. I now donot want the script to prompt and it should take defaul 'Y' and install all configuration. How do I make this happen ?
.load_cfg.done : %
#echo -e "\n* Loading configuration $<"
$(PIN_HOME)/cfg/load_config.sh $<
touch $#
Shell : BASH
OS : LINUX
Use the yes command, in order to send a certain string into a user prompt.
For example, say your script looks like this:
$ cat loadConfig.sh
#!/bin/bash
read -p "Load all configuration? " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Loading all configuration"
else
echo "Not loading all configuration"
fi
Pipe yes into your script:
$ yes | loadConfig.sh
Loading all configuration