How can I fix sed invalid command code '? - linux

I have a shell script with below 2 commands:
sedcmd1="sed -i '' '/<PromptOnUpdate>true<\/PromptOnUpdate>/G' /Users/abc/data.xml"
$sedcmd1
when I run the script I get below error:
sed: 1: "'/<PromptOnUpdate ...": invalid command code '
If I run the command without assigning it to the variable, it runs just fine. But I get an error only when I assign it to a variable an execute it from there.
I need the command in the variable for my needs, as I am try to receive the command as a argument to the shell script.
Snippet of data.xml:
<?xml version="1.0" ?>
<Settings Version="1.0.0">
<Android>
<PromptOnUpdate>true</PromptOnUpdate>
</Android>
</Settings>
Thanks for looking into this!
PS: I am on Mac OSX Mavericks

Quotes aren't processed after expanding a variable. You need to use eval:
eval "$sedcmd1"

The shell processes quotes before it expands variables. That means that the quotes inside your variable have no magic; they are treated the same as any other character.
To store a complex command as a variable, you need an array:
sedcmd1=(sed -i '' '/<PromptOnUpdate>true<\/PromptOnUpdate>/G' data.xml)
The array can be executed:
${sedcmd1[#]}
If you are unsure if you have defined your array character, you can always see what is in it using the declare command;
declare -p sedcmd1
which yields:
declare -a sedcmd1='([0]="sed" [1]="-i" [2]="" [3]="/<PromptOnUpdate>true<\\/PromptOnUpdate>/G" [4]="data.xml")'

Related

sh shell redirecting a subshell to file, can't find the right syntax

i want to run a sed command with programatically with changing parameters.
the thing is that i cant find the correct syntax to do so.
i want to configure a conf file with this and
change a dir path to another.
i'm currently using:
RESULT=$("sed 's/--ROOT_DIR--/${root_inst_dir}/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf > ${SOURCE_DIR}/${tool_name}.conf")
and i get the error message:
./change_tst.sh: line 7: sed 's/--ROOT_DIR--//home/test_dir/g' /home/tst/conf.conf > /home/script_tst/conf.conf: No such file or directory
the ">" is not working for some reason.
what am i doing wrong? or what is the best way to do this ?
UPDATE
i drooped the result variable and now running this:
(sed 's/--ROOT_DIR--/$root_inst_dir/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf) > ${SOURCE_DIR}/${tool_name}.conf
the new file is being created in > ${SOURCE_DIR}/${tool_name}.conf,
but the search/replace is happening literally and not as a variable...
thanks.
Putting " inside parenthesis will result in bash wanting to execute a command named exactly:
sed 's/--ROOT_DIR--/${root_inst_dir}/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf > ${SOURCE_DIR}/${tool_name}.conf"
Such command does not exist on your system.
Probably you intended to put " outside $(...):
RESULT="$(sed 's/--ROOT_DIR--/${root_inst_dir}/g' ${root_inst_dir}/${tool_name}/etc/${tool_name}.conf > ${SOURCE_DIR}/${tool_name}.conf)"
Better way, if you don't need the RESULT variable and if you want to properly escape root_inst_dir variable:
sed 's#--ROOT_DIR--#'"${root_inst_dir}"'#g' "${root_inst_dir}/${tool_name}/etc/${tool_name}.conf" > "${SOURCE_DIR}/${tool_name}.conf"
Or if you need RESULT variable:
sed 's#--ROOT_DIR--#'"${root_inst_dir}"'#g' "${root_inst_dir}/${tool_name}/etc/${tool_name}.conf" > "${SOURCE_DIR}/${tool_name}.conf"
RESULT=$(cat ${SOURCE_DIR}/${tool_name}.conf)

How to store command arguments which contain double quotes in an array?

I have a Bash script which generates, stores and modifies values in an array. These values are later used as arguments for a command.
For a MCVE I thought of an arbitrary command bash -c 'echo 0="$0" ; echo 1="$1"' which explains my problem. I will call my command with two arguments -option1=withoutspace and -option2="with space". So it would look like this
> bash -c 'echo 0="$0" ; echo 1="$1"' -option1=withoutspace -option2="with space"
if the call to the command would be typed directly into the shell. It prints
0=-option1=withoutspace
1=-option2=with space
In my Bash script, the arguments are part of an array. However
#!/bin/bash
ARGUMENTS=()
ARGUMENTS+=('-option1=withoutspace')
ARGUMENTS+=('-option2="with space"')
bash -c 'echo 0="$0" ; echo 1="$1"' "${ARGUMENTS[#]}"
prints
0=-option1=withoutspace
1=-option2="with space"
which still shows the double quotes (because they are interpreted literally?). What works is
#!/bin/bash
ARGUMENTS=()
ARGUMENTS+=('-option1=withoutspace')
ARGUMENTS+=('-option2=with space')
bash -c 'echo 0="$0" ; echo 1="$1"' "${ARGUMENTS[#]}"
which prints again
0=-option1=withoutspace
1=-option2=with space
What do I have to change to make ARGUMENTS+=('-option2="with space"') work as well as ARGUMENTS+=('-option2=with space')?
(Maybe it's even entirely wrong to store arguments for a command in an array? I'm open for suggestions.)
Get rid of the single quotes. Write the options exactly as you would on the command line.
ARGUMENTS+=(-option1=withoutspace)
ARGUMENTS+=(-option2="with space")
Note that this is exactly equivalent to your second option:
ARGUMENTS+=('-option1=withoutspace')
ARGUMENTS+=('-option2=with space')
-option2="with space" and '-option2=with space' both evaluate to the same string. They're two ways of writing the same thing.
(Maybe it's even entirely wrong to store arguments for a command in an array? I'm open for suggestions.)
It's the exact right thing to do. Arrays are perfect for this. Using a flat string would be a mistake.

dynamically creating bash variable and accessing its value defined earlier

i am trying to read the value of a bash variable defined earlier , but this variable name derived dynamically.
this is the bash script i am trying to do
$ mythreshold=10
$ table=my
$ threshold="$table"threshold
$ echo $("$threshold")
mythreshold
but when i try to read this variable value like
$ echo $("$threshold")
-bash: mythreshold: command not found
however i was expecting it to print
$ echo $("$threshold")
10
is there a way i can get this work, it should have printed the value of mythreshold variable defined above
$() is Command Substitution. It runs the command inside and returns the output. A variable name is not a command.
You can $(echo "$threshold") but that will only get the mythreshold back.
You need indirection for what you want. Specifically Evaluating indirect/reference variables.
As an example, for this specific case:
echo "${!threshold}"
Use eval command :
eval echo \${$threshold}
More details about this command can be found here:
eval command in Bash and its typical uses

Parsing a variable in shell scripting

I am new to shell scripting just started off.
I have written this script
#!/bin/sh
profile_type= cat /www/data/profile.conf
echo $profile_type
the o/p of this script is
. /tmp/S_panicA1.txt
. /tmp/S_panicA0.txt
away_Def="panicA1 panicA0"
away_Byp=0
away_Sts=$((panicA1+panicA0-away_Byp))
In this i want to get panicA1 panicA0 and 0 and store it in other variable how to do this?
When you want to assign the output of a command to a variable, you use the dollar parenthesis syntax.
foo=$(cat /my/file)
You can also use the backticks syntax.
foo=`cat /my/file`
In your script, you simply run the command cat and assign its result, nothing, to your variable. Hence the output consisting of the content of your file, result of cat, followed by an empty line, result of echo with an empty variable.

Bash Shell - The : Command

The colon command is a null command.
The : construct is also useful in the conditional setting of variables. For example,
: ${var:=value}
Without the :, the shell would try to evaluate $var as a command. <=???
I don't quite understand the last sentence in above statement. Can anyone give me some details?
Thank you
Try
var=badcommand
$var
you will get
bash: badcommand: command not found
Try
var=
${var:=badcommand}
and you will get the same.
The shell (e.g. bash) always tries to run the first word on each command line as a command, even after doing variable expansion.
The only exception to this is
var=value
which the shell treats specially.
The trick in the example you provide is that ${var:=value} works anywhere on a command line, e.g.
# set newvar to somevalue if it isn't already set
echo ${newvar:=somevalue}
# show that newvar has been set by the above command
echo $newvar
But we don't really even want to echo the value, so we want something better than
echo ${newvar:=somevalue}.
The : command lets us do the assignment without any other action.
I suppose what the man page writers meant was
: ${var:=value}
Can be used as a short cut instead of say
if [ -z "$var" ]; then
var=value
fi
${var} on its own executes the command stored in $var. Adding substitution parameters does not change this, so you use : to neutralize this.
Try this:
$ help :
:: :
Null command.
No effect; the command does nothing.
Exit Status:
Always succeeds.

Resources