I'm struggling with this code:
komanda=$(dialog --title "COMMAND" --backtitle "ENTER COMMAND: "
--inputbox "" 8 180 2>&1 >/dev/tty)
if [ $? == 0 ] then
for ((j=0;j<$tlen;j++))
do
shopnum=${selectedRPODS[j]}
$komanda #executing the entered comand with variables.
done
fi
When I enter a variable in the inputbox, the parameter is passed on as a string. Variables are not working when execute at $komanda
Example input text:
ping $shopnum
returns always:
ping: unknown host $shopnum
I wanted to have the variable as destination for the ping. I know its something I'm doing wrong with the STDOUT but I can't find the right way to do it.
Thanks in advance.
Parameter expansions are not recursive. If you enter ping $shopnum, then $komanda expands to the literal text ping $shopnum, not ping 3 or whatever value was assigned to shopnum.
In this case, since your intention is to execute arbitrary shell code, you would use eval:
eval "$komanda"
However, it would be better to rethink your design so that the user doesn't need to know what variable names exist in the code.
Related
I have a custom CLI command that executes and requires user to enter a password. If the password fails authentication, it will echo some message like "Authentication failed" and I would like to see if the outputted string contains some substring like "fail". However, I noticed that capturing the "output" of a command is not giving me the text it echos on the terminal screen. If I would like to read through all the text the command echoed which doesn't technically come into the "output" bucket, how would I go about doing that?
Example:
> custauth
Enter your password:
Checking if password is correct...
Authentication Failed. Please try again
In above example you can see that it doesn't have an "output" but generates text that is displayed to the user. I would like to capture those lines and check for a substring in them rather than its official output
Using $? only gives me the exit code but oftentimes the CLI command can fail gracefully and output 0, so I'll need to read through all the statements it echoed.
The process can be passed initially by checking the status correctly, which is through
#!/bin/bash
# We put here any code that we check for verification
#like list files to fast exapmle
ls
cpstatus=$?
if [ "$cpstatus" == 0 ]; then
echo "complete"
else
echo "not complete"
fi
wait # Until the upper commands is finish
Or if you know what you are achieving, you can check the way out through it, and compare if there is a part of the speech that shows us a specific sentence, for example, if we return a failure, you will add that to the condition
I will attach an example code I wrote to infer the name of the system, for example:
System=$(uname -a) #str=$(command)
Substring ='Linux'
if [[ "$System" == *"$Substring"* ]]; then
echo "Yes is contain $Substring"
fi
I hope the solutions helped you, good luck :)
I want to log into server based on user's choice so I wrote bash script. I am totally newbie - it is my first bash script:
#!/bin/bash
echo -e "Where to log?\n 1. Server A\n 2. Server B"
read to_log
if [ $to_log -eq 1 ] ; then
echo `ssh user#ip -p 33`
fi
After executing this script I am able to put a password but after nothing happens.
If someone could help me solve this problem, I would be grateful.
Thank you.
The problem with this script is the contents of the if statement. Replace:
echo `ssh user#ip -p 33`
with
ssh user#ip
and you should be good. Here is why:
Firstly, the use of back ticks is called "command substitution". Back ticks have been deprecated in favor of $().
Command substitution tells the shell to create a sub-shell, execute the enclosed command, and capture the output for assignment/use elsewhere in the script. For example:
name=$(whoami)
will run the command whoami, and assign the output to the variable name.
the enclosed command has to run to completion before the assignment can take place, and during that time the shell is capturing the output, so nothing will display on the screen.
In your script, the echo command will not display anything until the ssh command has completed (i.e. the sub-shell has exited), which never happens because the user does not know what is happening.
You have no need to capture the output of the ssh command, so there is no need to use command substitution. Just run the command as you would any other command in the script.
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
!
I have created a simple shell script. In this script one command require user input as 1 1 1 ENTER ENTER ENTER ENTER etc.
How can i pass these user inputs to script?
Regards,
Ankit
If you want to pass three lines consisting of the string "1" followed by an arbitrary number of blank lines to the command cmd, you can do:
yes "" | sed -e 1,3s/^/1/ | cmd
What shell are you using?
Typically ,You can pass user input using read.
Here is an example using bash.
#!/bin/bash
# Ask User to Input Data
echo "Enter your Number"
#Store user input to a variable
read userNumber
#Process user input. In this case display it back
echo "You entered $userNumber"
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.