This question already has answers here:
Expansion of variables inside single quotes in a command in Bash
(8 answers)
Closed 5 years ago.
I am trying to automate a GroupMe bot as simply as possible. An easy way to send a message from the command line is to use the following command:
curl -d '{"text" : "Your message here", "bot_id" : "this_is_a_secret_string"}' https://api.groupme.com/v3/bots/post
In a Shell script, I would like to replace "Your message here" with var, in which var is being set to the output from a different command. Is this possible?
Things I have replaced "Your message here" with that did not work:
var
$var
(var)
$(var)
{var}
${var}
Anything put within double quotes ("") is treated as a String, so did not try much in those regards.
The var will not be evaluated because it's w/in single quotes. One way around this is to just smash 3 strings together:
curl -d '{"text" : "'"$var"'Your message here", "bot_id" : "this_is_a_secret_string"}' https://api.groupme.com/v3/bots/post
string 1: '{"text" : "'
string 2: "$var"
string 3: 'Your message here", "bot_id" : "this_is_a_secret_string"}'
NOTE: this will only work if the contents of var are very simple. The expanded string must still be a valid JSON string.
Related
This question already has answers here:
How do you execute an arbitrary native command from a string?
(4 answers)
Closed 6 months ago.
Let's say I want to get a specific dir contents:
$cmd = "dir"
$args = ".yarn*"
$output = "$cmd $args"
echo $output
My output contains 2 string literals:
dir .yarn*
Expected (as a command on 1 line, opposed to a "string" on separate lines):
dir *yarn
#iRon 's comment with a suggested answer was correct:
Invoke-Expression($output) is the answer!
I have this script called test.sh:
#!/bin/bash
STR = "Hello World"
echo $STR
when I run sh test.sh I get this:
test.sh: line 2: STR: command not found
What am I doing wrong? I look at extremely basic/beginners bash scripting tutorials online and this is how they say to declare variables... So I'm not sure what I'm doing wrong.
I'm on Ubuntu Server 9.10. And yes, bash is located at /bin/bash.
You cannot have spaces around the = sign.
When you write:
STR = "foo"
bash tries to run a command named STR with 2 arguments (the strings = and foo)
When you write:
STR =foo
bash tries to run a command named STR with 1 argument (the string =foo)
When you write:
STR= foo
bash tries to run the command foo with STR set to the empty string in its environment.
I'm not sure if this helps to clarify or if it is mere obfuscation, but note that:
the first command is exactly equivalent to: STR "=" "foo",
the second is the same as STR "=foo",
and the last is equivalent to STR="" foo.
The relevant section of the sh language spec, section 2.9.1 states:
A "simple command" is a sequence of optional variable assignments and redirections, in any sequence, optionally followed by words and redirections, terminated by a control operator.
In that context, a word is the command that bash is going to run. Any string containing = (in any position other than at the beginning of the string) which is not a redirection and in which the portion of the string before the = is a valid variable name is a variable assignment, while any string that is not a redirection or a variable assignment is a command. In STR = "foo", STR is not a variable assignment.
Drop the spaces around the = sign:
#!/bin/bash
STR="Hello World"
echo $STR
In the interactive mode everything looks fine:
$ str="Hello World"
$ echo $str
Hello World
Obviously(!) as Johannes said, no space around =. In case there is any space around = then in the interactive mode it gives errors as
No command 'str' found
I know this has been answered with a very high-quality answer. But, in short, you cant have spaces.
#!/bin/bash
STR = "Hello World"
echo $STR
Didn't work because of the spaces around the equal sign. If you were to run...
#!/bin/bash
STR="Hello World"
echo $STR
It would work
When you define any variable then you do not have to put in any extra spaces.
E.g.
name = "Stack Overflow"
// it is not valid, you will get an error saying- "Command not found"
So remove spaces:
name="Stack Overflow"
and it will work fine.
I'm practicing bash and honestly, it is pretty fun. However, I'm trying to write a program that compares an array's value to a variable and if they are the same then it should print the array's value with an asterisk to the left of it.
#!/bin/bash
color[0]=red
color[1]=blue
color[2]=black
color[3]=brown
color[4]=yellow
favorite="black"
for i in {0..4};do echo ${color[$i]};
if {"$favorite"=$color[i]}; then
echo"* $color[i]"
done
output should be *black
There's few incorrect statements in your code that prevent it from doing what you ask it to. The comparison in bash is done withing square brackets, leaving space around them. You correctly use the = for string comparison, but should enclose in " the string variable. Also, while you correctly address the element array in the echo statement, you don't do so inside the comparison, where it should read ${color[$i]} as well. Same error in the asterisk print. So, here a reworked code with the fixes, but read more below.
#!/bin/bash
color[0]=red
color[1]=blue
color[2]=black
color[3]=brown
color[4]=yellow
favorite=black
for i in {0..4};do
echo ${color[$i]};
if [ "$favorite" = "${color[$i]}" ]; then
echo "* ${color[$i]}"
fi
done
While that code works now, few things that probably I like and would suggest (open to more expert input of course by the SO community): always enclose strings in ", as it makes evident it is a string variable; when looping an array, no need to use index variables; enclose variables always within ${}.
So my version of the same code would be:
#!/bin/bash
color=("red" "blue" "black" "brown" "yellow")
favorite="black"
for item in ${color[#]}; do
echo ${item}
if [ "${item}" = "${favorite}" ]; then
echo "* $item"
fi
done
And a pointer to the great Advanced Bash-Scripting Guide here: http://tldp.org/LDP/abs/html/
How can I get the raw command line arguments in a node.js app,
given this example command:
node forwardwithssh.js echo "hello arguments"
process.argv will be [ "node", "/path/to/forwardwith.js", "echo", "hello arguments" ]
And there's no way to reconstruct the original echo "hello arguments" from that (ie. join(" " won't put quotes back).
I want the whole original raw string after the script name.
what I want is easily obtained in bash scripts with "$*", is there any equivalent way to get that in node.js?
Note: the intention in particular is to receive a command to be executed somewhere else (eg. thru ssh)
Wrap each of the args in single quotes, and escape and single quotes within each arg to '\'':
var cmd_string = process.argv.map( function(arg){
return "'" + arg.replace(/'/g, "'\\''") + "'";
}).join(' ');
That would give you a cmd_string containing:
'node' '/path/to/forwardwith.js' 'echo' 'hello arguments'
which can be run directly in another shell.
This question already has answers here:
Usage of :- (colon dash) in bash
(2 answers)
Closed 9 years ago.
Sorry if this is something really simple or has already been asked, but due to the nature of the question I cannot think of any search terms to put on search engines.
Lately I have seen some bash scripts that they assign variable values like this:
$ MY_BASH_VAR=${MY_BASH_VAR:-myvalue}
$ echo "$MY_BASH_VAR"
myvalue
What is the difference from the most common way of assigning a value like this:
MY_BASH_VAR=myvalue
$ echo "$MY_BASH_VAR"
myvalue
You can look at http://linux.die.net/man/1/bash
${parameter:-word} Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
This provides a default value : MY_BASH_VAR keeps its value if defined otherwise, it takes the default "myvalue"
bruce#lorien:~$ A=42
bruce#lorien:~$ A=${A:-5}
bruce#lorien:~$ echo $A
42
Suppose the $MY_BASH_VAR was already set. In this case, it will keep the same value. If not, it will get myvalue.
case 1) $MY_BASH_VAR already set.
$ MY_BASH_VAR="hello"
$ MY_BASH_VAR=${MY_BASH_VAR:-myvalue}
$ echo "$MY_BASH_VAR"
hello
case 2) $MY_BASH_VAR not previously set.
$ MY_BASH_VAR=${MY_BASH_VAR:-myvalue}
$ echo "$MY_BASH_VAR"
myvalue
case 3) $MY_BASH_VAR set to the empty string.
$ MY_BASH_VAR=""
$ MY_BASH_VAR=${MY_BASH_VAR:-myvalue}
$ echo "$MY_BASH_VAR"
myvalue