How to follow _ after $ in bash? - bash

I have a shell script that need to use a variable following a dash to generate the file names.Here is what I did:
var="5"
echo "filename$var_something.txt"
However, it always output error, because the script treats $var_something as a variable.
How can I solve this?
Thanks.

echo "filename${var}_something.txt" use braces to delimit your variable name.

Related

bash replace variable name in string with variable value

This is kind of a weird one. I have the following string:
I have a variable called REDIRECT set to: https://working.${MYDOMAIN}/blah/blah.
I need to replace the ${MYDOMAIN} with the actual value of the variable assigned to ${MYDOMAIN}.
Not sure if bash or sed is best for this. I tried bash replace but couldn't get it to work, probably related to escaping the characters or something. Any help appreciated!
You may use this bash substitution:
echo "${REDIRECT/\${MYDOMAIN\}/$MYDOMAIN}"
or else, if you have envsubst utility then use:
export MYDOMAIN
envsubst <<< "$REDIRECT"
Just execute in bash:
eval REDIRECT=$REDIRECT

Shell command date

I want to print date in the mm-dd-yy format in shell script. From shell terminal I can get it using the following command:
date +"%d-%m-%y"
But I want it in the shell script and in a variable which could then be appended to a file name. I tried the following:
#!/bin/sh
mydate=`"date +\"%m-%d-%Y\""'
echo "$mydate"
But it is giving an error date +"%d-%m-%y" is not found.
Can anybody point out what mistake am I making?
You don't need quotes
mydate=`date +%m-%d-%Y`
will work.
Use
mydate=$(date "+%m-%d-%Y")
See this is a way to store a command in a variable: var=$(command). To use date, you define the format like date "+%format%place%holders", with + inside the double quotes.
$ mydate=$(date "+%m-%d-%Y")
$ echo $mydate
09-29-2014
Note it is preferred to use $() over ``, because it allows nesting multiple commands.
You have advice about how to do it properly. The reason for the error is the first level of inner double quotes makes the entire command with arguments into a single word:
mydate=`"date +\"%m-%d-%Y\""'
You are trying to execute a command named:
date +"%m-%d-%Y"
and clearly no such command exists.
#!/bin/sh
mydate=`date +\"%m-%d-%Y\"`
mydate1=`date +%m-%d-%Y`
echo "$mydate"
echo "$mydate1"
Both approaches will work. But the first one will have the date value surrounded by double quotes, something like "09-29-2014".

Difficulty executing shell script with directory reference

I have a simple script
...
dir=`pwd`
echo $dir
cd ./selenium-grid-1.0.8/
CMD="ant -Dport=$1 -Dhost=$2 -DhubURL=http://172.16.1.137:4444 -Denvironment="$3"-DseleniumArgs="-firefoxProfileTemplate C:/software/rc_user_ffprofile -multiWindow" launch-remote-control"
echo $CMD
$CMD 2>&1
#End
Whenever i run this command, i get: ./register_rc.sh: line 16: C:/software/rc_user_ffprofile: is a directory
this directory has to be an argument to the -firefoxProfileTemplate option. How do i include that in this string without it baffing??
help
thnx
I believe your command should read:
CMD="ant -Dport=$1 -Dhost=$2 -DhubURL=http://172.16.1.137:4444 -Denvironment=\"$3\"-DseleniumArgs=\"-firefoxProfileTemplate C:/software/rc_user_ffprofile -multiWindow\" launch-remote-control"
The backslashes are used to "escape" the quotation marks.
The answers here telling to escape your quotes are wrong. That will pass those quotes directly to ant, I doubt that's what you want.
What's the reason to store the command in a variable? It's a very bad idea. Why can't you just write that command as is? If you want to achieve modularity or code reuse, then define a function.
If you want to display executed commands, use set -x.
Looks like you're mixing your quotes up. Take a look at the syntax highlighting that StackOverflow did for you.
I recommend generating the CMD variable in multiple steps, and make sure you \-escape your quotes.

Bash script doesn't set variable when run from script, but works fine from prompt

Why does the following work from the prompt but fail when stuck inside a bash script? The bash script produces one empty line leading me to believe the variable isn't being set:
echo "red sox" | read my_var
echo $my_var
UPDATE: Since I guess my example isn't working, what I'm really trying to do is take I/O and pipe it into a variable to so I can do things with it. How do I do this? I thought this should be taken care of by the read command, but maybe there's another way?
If you are asking this because you simplified from a more general problem such as:
someprog args | read my_var
you should be using command substitution:
my_var=$(someprog args)
The reason read doesn't work in a pipe the way you have it is that it creates a subshell. Variables set in a subshell don't persist to their parents. See BashFAQ/024.
You can store the output of a command in a variable using either backticks or $() syntax:
my_var=`cat /some/file.txt`
Now the content of /some/file.txt is stored in $my_var. Same thing, with different syntax:
my_var=$(cat /some/file.txt)
It doesn't work at the prompt. My guess is you already have my_var set in your CLI, and are just retrieving that at the prompt.
Try this:
$ my_var="nothing"; echo "red sox" | read my_var; echo $my_var
If you want the my_var variable to have a constant value, as in your question, why not just do:
my_var="red sox"
What are you trying to do? Please explain what you wan't to do first.
If you're trying to set a variable this is what you need:
my_var="red sox"
echo $my_var
If you need it globally you should set it in env:
export my_var

Problem in running a script

i have unix shell script which is need to be run like below
test_sh XYZ=KLMN
the content of the script is
#!/bin/ksh
echo $XYZ
for using the value of XYZ i have do set -k before i run the script.
is there a way where i can do this without doint set -k before running the script. or is there something that i can do in the script where i can use value of the parameter given while running the script in the below way
test_sh XYZ=KLMN
i am using ksh.
Any help is appreciated.
How about running this?
XYZ=KLMN ./test_sh //running from directory where test_sh is
If your script needs no other arguments, a quick and dirty way do to it is to put
eval "$#"
at the start of your script. This will evaluate the command line arguments as shell commands. If those commands are to assign a shell/environment variable, then that's what it will do.
It's quick-and-dirty since anything could be put on the command line, causing problems from a syntax error to a bad security hole (if the script is trusted).
I'm not sure if "$#" means the same in ksh as it does in bash - using just $* (without quotes) would work too, but is even dirtier.
It looks like you are trying to use the environment variable "INSTANCE" in your script.
For that, the environment variable must be set in advance of executing your script. Using the "set" command sets exportable environment variables. Incidentally, my version of ksh dates from 1993 and the "-k" option was obsolete back then.
To set an environment variable so that it is exported into spawned shells, simply use the "export" command like so:
export INSTANCE='whatever you want to put here'
If you want to use a positional parameter for your script -- that is have the "KLMN" value accessed within your script, and assuming it is the first parameter, then you do the following in your script:
#!/bin/ksh
echo $1
You can also assign the positional parameter to a local variable for later use in your script like so:
#!/bin/ksh
param_one=$1
echo $param_one
You can call this with:
test_sh KLMN
Note that the spacing in the assignment is important -- do not use spaces.
I am tring this option
#!/bin/ksh
echo $1
awk '{FS="=";print $2}' $1
and on the command line
test_sh INSTANCE=LSN_MUM
but awk is failing.is there any problem over here?
Probably #!/bin/ksh -k will work (untested).

Resources