Given this code:
L_count=12
echo "hi hello $l_count"
The expected output is:
hi hello 12
If I run the code it gives the result as desired.
However, I have problems working in databases:
Now I am inserting the same string 'hi hello $l_count' in col1 of test table:
Insert into test(col1) values ('hi hello $l_count');
In UNIX:
l_count=12
var1=sqlconnect
Select col1 from test
echo $var1
It is not replacing l_count with 12 even if I do:
echo $(echo $var1)
How can I handle this?
If I understand your problem correctly, you have a code fragment in your database that you want to evaluate as a shell script. You don't mention what shell you are using, I am guessing bash.
Bash contains a builtin function called eval to help you do that. I guess other shells probably have similar functionality.
So basically, try eval echo $var1
Thanks peder.Below command worked fine
eval "echo //$var1"
Related
I have a C program that I want to run without having to manually type commands into. I have 4 commands (5 if you count the one to exit the program) that I want given to the program and I don't know where to start. I have seen some stuff like
./a.out <<<'name'
to pass in a single string but that doesn't quite work for me.
Other issues I have that make this more difficult are that one of the commands will give an output and that output needs to be a part of a later command. If I had access to the source code I could just brute force in some loops and counters so I am trying to get a hold of it but for now I am stuck working without it. I was thinking there was a way to do this with bash scripts but I don't know what that would be.
In simple cases, bash script is a possibility: run the executable in coproc (requires version 4). A short example:
#!/bin/bash
coproc ./parrot
echo aaa >&${COPROC[1]}
read result <&${COPROC[0]}
echo $result
echo exit >&${COPROC[1]}
with parrot (a test executable):
#!/bin/bash
while [ true ]; do
read var
if [ "$var" = "exit" ]; then exit 0; fi
echo $var
done
For a more serious scenarios, use expect.
I try to transfer the excellent example docker-haproxy from centos to alpine.
A shell script is used to process a list of values given as parameters to the script into an array, then write these values plus their index to some file.
The following construction works in bash:
ServerArray=${SERVERS:=$1}
...
for i in ${ServerArray[#]}
do
echo " " server SERVER_$COUNT $i >> /haproxy/haproxy.cfg
let "COUNT += 1"
done
but not in ash (or sh):
syntax error: bad substitution
The error refers to line
for i in ${ServerArray[#]}
What is the correct syntax here? I guess the line
ServerArray=${SERVERS:=$1}
does not define an array as intended, but googling for long did not help me.
bash to sh (ash) spoofing says
sh apparently has no arrays.
If so, how to solve the problem then?
I guess I can do with this construction:
#!/bin/sh
# test.sh
while [ $# -gt 0 ]
do
echo $1
shift
done
delivers
/ # ./test 172.17.0.2:3306 172.17.0.3:3306
172.17.0.2:3306
172.17.0.3:3306
which is what I need to proceed
I am having some trouble with a simple shell script on my ubuntu box. Here is the first part of the script:
#!/bin/sh
LCL="192.168.1.1/24"
VPN="10.0.0.0/12"
local_interface="eth0"
virtual_interface="tun0"
servers=(
199.315.117.225
46.31.151.106
46.31.154.82
)
I run the script like this:
sudo sh script.sh
And this is the output:
script.sh: 6: script.sh: Syntax error: "(" unexpected
As far as I can tell, that is a valid array, so I don't understand why that paren is unexpected. I am a newbie so any help is much appreciated.
The problem is that you're running your script using a shell (namely Dash) that doesn't support a feature your script is using (namely Bash-style arrays).
The easiest fix is to change this:
#!/bin/sh
to this:
#!/bin/bash
so that your script is run using Bash instead of Dash.
/bin/bash is superset of /bin/sh... This array syntax works in bash.
https://superuser.com/questions/125728/what-is-the-difference-between-bash-and-sh
http://www.linuxjournal.com/content/bash-arrays
In shell script array should be mentioned by
bash:
declare -a varname
varname=( 192.168.1.1 192.168.1.2 192.168.1.2 )
and you can get all ip by
echo "${varname[#]}"
or
echo "${varname[*]}"
you can try the below code,
#!/bin/sh
LCL="192.168.1.1/24"
VPN="10.0.0.0/12"
local_interface="eth0"
virtual_interface="tun0"
ARRAY="199.315.117.225 46.31.151.106 46.31.154.82"
for i in $ARRAY
do
echo $i
done
I have a program written in C that operates similar to the below output.
WELCOME TO PROGRAM.
Hit 1 to do task 1.
Hit 2 to do task 2.
Hit q to quit.
What i need is a bash shell script that start the program, then enters 1, 2 and q into the program so i can test all the functionality in one command.
I would assume it to look similar to the following
#!/bin/bash
./start kernel
1
2
q
You can use a "here document" . The syntax looks like this:
./start kernel <<EOF
1
2
q
EOF
"EOF" can be whatever unique word you want, as long as it isn't something you'll actually need in the input.
Typically you use expect for testing these types of applications.
You can save your input in a text file - input.txt and execute your program this way: ./program < input.txt
I do this:
#! /bin/bash
printf "1\n2\nq\n" | ./start kernel
You can think of shell scripts as what they are... just each line being executed in an (albeit new) shell.
A simple way to do this sort of input is, assuming [your program] accepts stdin, is:
#!/bin/bash
echo "1" | [your program] > [logfile1]
echo "2" | [your program] > [logfile2]
echo "q" | [your program] > [logfileq]
The script I'm writing will require me to pass some command line parameters. I would like to use these parameters within an array, but I'm not sure how.
A very basic example of this would be (script run as ./script.sh array1):
#!/bin/bash
array1=( a b c d )
echo ${#$1[#]}
The output should be 4, but I receive the following error:
line 5: ${#$1[#]}: bad substitution.
I don't have to use arrays, but would like to.
Thanks for any ideas
you need to get bash to substitute the value of $1 before evaluating the line, try this...
eval echo \${#$1[#]}
eval echo '${#'$1'[#]};'