How a pass a value with $$ in it to a shell script - shell

e.g ksh myshell.sh parm1, $$param2.
I want to assign the second parameter to a variable inside my shell script
e.g. var1=$2
and then use it as $var1,
i.e. var1 should have value $$param2 when I use it as $var1 inside my shell script.

That sounds about right. You just need to make sure you quote the call to my shell.sh correctly, so that the shell doesn't expand the $$ before your script is even called.
ksh myshell.sh parm1 '$$param2'
or
ksh myshell.sh parm1 $\$param2
Note that you don't need to escape both dollar signs, although you can if you like. It is not sufficient to escape only the first one, since $param2 would still be expanded.

Related

Expand a variable within variable right before echo

Example:
testing="test"
var="\"${testing} the variable\""
testing="Update"
echo "$var"
Output:
test the variable
Required output:
Update the variable
Variables are expanded when the string is used, they're not templates that remember the variable substitution.
If you need to do this, you need to put the variable literally into the string, and use eval.
testing="test"
var='"${testing} the variable"'
testing="Update"
eval "echo $var"
But eval is dangerous -- it will execute any shell commands in the string. A better solution would probably be to use some kind of placeholder string and replace it using the shell expansion operator.
var='#testing# the variable'
testing="Update"
echo "${var//#testing#/$testing}"

Assign ASCII values to variable in shell

I'm still new at shell scripting
I want to assign * to a variable an print it. Write now I'm just printing it with:
echo -e "\052"
Is there a way to assign that value to a variable?
Use $(cmd) or `cmd` to capture a command's output. The $(...) form is preferred because it's easier to nest.
var=$(echo -e "\052")
The shell will interpret escape sequences inside $'...'. That's single quotes with a dollar sign in front.
var=$'\052'
Or of course you could write the asterisk directly. Quote it to prevent wildcard expansion.
var='*'
When you print it, make sure to quote the variable. It's annoying to always have to type double quotes any time you use a variable, but it's usually the right thing to do.
echo "$var" # yes
echo $var # no
Using backticks, ``, allows you to capture the output of a command. Many shells have a more sophisticated syntax, $(). But backticks are the most portable.
var=`echo -e "\052"`

Stop bash from expanding $ from command line

I have a script I am trying to call that needs to have the $ symbol passed to it. If I run the script as
./script "blah$blah"
it is passed in fine but then the script calls another program I have no control over which then expands the parameter to just "blah". The program is being called by the command program $#. I was wondering if there was a way to prevent the parameter from being expanded when passed to the next script.
Escape the character $ with: \, e.g.: "This will not expand \$hello"
use single quotes: 'This will not expand $hello'
Use a HERE DOC:
<<'EOF'
This will not expand $hello
EOF
In your case I recommend using single quotes for readability: ./script 'blah$blah'.
A couple of options involving changing the quoting:
./script 'blah$blah'
./script "blah\$blah"
I hope this helps.
Call using single quotes:
./script 'blah$blah'
Or escape the $
./script "blah\$blah"

How do I embed an expect script that takes in arguments into a bash shell script?

I am writing a bash script which amongst many other things uses expect to automatically run a binary and install it by answering installer prompts.
I was able to get my expect script to work fine when the expect script is called in my bash script with the command "expect $expectscriptname $Parameter". However, I want to embed the expect script into the shell script instead of having to maintain two separate script files for the job. I searched around and found that the procedure to embed expect into bash script was to declare a variable like VAR below and then echo it.:
VAR=$(expect -c "
#content of expect script here
")
echo "$VAR"
1) I don't understand how echoing $VAR actually runs the expect script. Could anyone explain?
2) I am not sure how to pass $Parameter into VAR or to the echo statement. This is my main concern.
Any ideas? Thanks.
Try something like:
#!/bin/sh
tclsh <<EOF
puts $1
EOF
I don't have the expect command installed today, so I used tclsh instead.
In bash, the construct $(cmd) runs the specified command and captures its output. It's similar to the backtick notation, though there are some slight differences. Thus, the assignment to VAR is what runs the expect command:
# expect is run here
VAR=$(expect -c "
# ...
")
# This echoes the output of the expect command.
echo "$VAR"
From the bash manual page:
When the old-style backquote form
of substitution is used, backslash
retains its literal meaning except
when followed by $, , or \. The
first backquote not preceded by a
backslash terminates the command
substitution. When using the
$(command)` form, all characters
between the parentheses make up the
command; none are treated specially.
That's why it works: The bash comment character (#) isn't treated as a comment character within the $( ... ).
EDIT
Passing parameters: Just put 'em in there. For instance, consider this script:
foo="Hello, there"
bar=$(echo "
# $foo
")
echo $bar
If you run that script, it prints:
# Hello, there
Thus, the value of $foo was substituted inside the quotes. The same should work for the expect script.
Instead of a bash script and an expect script, have you considered writing just a single expect script?
Expect is a superset of Tcl, which means it is a fully functioning programming language, and you can do anything with it that you can do with bash (and for the things that you can't, you can always exec bash commands. You don't have to use expect just to "expect" things

How to echo a variable containing an unescaped dollar sign in bash

If I have a variable containing an unescaped dollar sign, is there any way I can echo the entire contents of the variable?
For example something calls a script:
./script.sh "test1$test2"
and then if I want to use the parameter it gets "truncated" like so:
echo ${1}
test1
Of course single-quoting the varaible name doesn't help. I can't figure out how to quote it so that I can at least escape the dollar sign myself once the script recieves the parameter.
The problem is that script receives "test1" in the first place and it cannot possibly know that there was a reference to an empty (undeclared) variable. You have to escape the $ before passing it to the script, like this:
./script.sh "test1\$test2"
Or use single quotes ' like this:
./script.sh 'test1$test2'
In which case bash will not expand variables from that parameter string.
The variable is replaced before the script is run.
./script.sh 'test1$test2'
by using single quotes , meta characters like $ will retain its literal value. If double quotes are used, variable names will get interpolated.
As Ignacio told you, the variable is replaced, so your scripts gets ./script.sh test1 as values for $0 and $1.
But even in the case you had used literal quotes to pass the argument, you shoudl always quote "$1" in your echo "${1}". This is a good practice.

Resources