In Powershell, how to turn a string into a cmd? [duplicate] - string

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!

Related

Replacing a string with a variable in a cURL command [duplicate]

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.

Linux - Check if string is in list [duplicate]

This question already has answers here:
Check if a variable exists in a list in Bash
(21 answers)
Closed 5 years ago.
I have a bash script and I want to check if a string is in a list.
Like: string = "Hello World!", List=("foo", "bar").
Python example:
if name in list: # Another way -> if name in ["foo", "bar"]
# work to do
else:
sys.exit(1)
Thank you!
There are a number of ways, the simplest I see is:
#!/bin/sh
WORD_LIST="one two three"
MATCH="twox"
if echo "$WORD_LIST" | grep -qw "$MATCH"; then
echo "found"
else
echo "not found"
exit 1
fi

Accessing Perl array in bash [duplicate]

This question already has answers here:
Set a shell array from an array in Perl script
(3 answers)
Closed 6 years ago.
I have a perl code where I execute some bash commands using backticks. I want to read a perl array in that bash command. My array has some strings and I want to read them in a for loop of bash.
my #aArray = (1,2,3,4);
my $command = 'for i in $#aArray; do xxxxx $i; done;';
`$command`
I also want to catch error if any part of the loop fails. Thanks
As #chepner has suggested, the code you want looks a bit like this:
my #array = (1, 2, 3, 4);
for my $val (#array) {
# Pick your favourite/the most appropriate mechanism for making system calls
system("command", $val);
}
If you need to make a single call on a remote system, what you could do is something like this:
my #array = (1, 2, 3, 4);
my $command = "for i in ("
for my $val(#array) {
$val =~ s/(?<!\\) /\\ /g; # Escape spaces that haven't already been (if the array elements might contain them)
$command = "$command $val";
}
$command = $command."); do <command> $i; done;";
system($command);

Bash declaring variable with a number inside a for loop [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 7 years ago.
I can't find an answer to this problem, other than people asking to use an array instead. That's not what I want. I want to declare a number of variables inside a for loop with the same name except for an index number.
I=0
For int in $ints;
Do i=[$i +1]; INTF$i=$int; done
It doesn't really work. When I run the script it thinks the middle part INTF$i=$int is a command.
Without an array, you need to use the declare command:
i=0
for int in $ints; do
i=$((i +1))
declare "intf$i=$int"
done
With an array:
intf=()
for int in $ints; do
intf+=( $int )
done
Bash doesn't handle dynamic variable names nicely, but you can use an array to keep variables and results.
[/tmp]$ cat thi.sh
#!/bin/bash
ints=(data0 data1 data2)
i=0
INTF=()
for int in $ints
do
((i++))
INTF[$i]=$int
echo "set $INTF$i INTF[$i] to $int"
done
[/tmp]$ bash thi.sh
set 1 INTF[1] to data0

Command not found [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 7 years ago.
I am new to shell scripting
Below is my script
#!/bin/bash
first_num = 0
second_num = 0
echo -n "Enter the first number =>"
read first_num
echo -n "Enter the second number =>"
read second_num
echo "first + second = $((first_num + second_num))"
Whenever I run it, it prints like below
/Users/haani/arithmetic.bash: line 3: first_num: command not found
/Users/haani/arithmetic.bash: line 4: second_num: command not found
Enter the first number =>
What could be the reason for command not found here?
try without spaces:
first_num=1

Resources