how to write bch commands in bash file [duplicate] - bash

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
How to assign the output of a Bash command to a variable? [duplicate]
(5 answers)
Closed 4 years ago.
I want to write a bash script to get block count . It is giving error
./script.sh: line 4: =: command not found
Below is my script
#!/bin/bash
# getblockcount
$blockcount = bitcoin-cli getblockcount
echo $blockcount
Kindly tell what is wrong .

There should not be space around the operator.. So remove the space around = and thing should work. Also there are some other bits.. Here is the corrected one.. Ensure command bitcoin-cli getblockcount from terminal gives right result.
#!/bin/bash
# getblockcount
blockcount=$(bitcoin-cli getblockcount)
echo $blockcount

Related

Bash - length of command line argument $0 [duplicate]

This question already has answers here:
How to create shell variable with dashes?
(2 answers)
Closed 1 year ago.
I want to find the length of $0
If I use the following bash code
leading-space=${#0}
I get the following result:
./Install: line 25: leading-space=9: command not found
$0 has the value ¨./Install¨
The length of the string appears to be correct, but then bash gets confused. I am running bash 5.0.17 on Ubuntu 20.10. Can anyone see what I am doing wrong?
Identifiers in bash may not contain -, therefore the whole assignment is interpreted as a command (like cd, or grep) but there is no command with the name leading-space=9 on your system, resulting in the error.
Use the following:
leading_space=${#0}

Find out what the (shell) script was invoked with [duplicate]

This question already has answers here:
How do I parse command line arguments in Bash?
(40 answers)
How to get exact command line string from shell?
(2 answers)
Closed 3 years ago.
Suppose my script.sh could take a number of options and arguments. What is the best way to find out what the script was invoked with (form inside the script)?
For eg., someone called it with script.sh --foo_option bar_arg
Is there a way to echo that exact command they typed from inside the script?
I've tried echo !! which does not work inside a script.

Can't understand how params are set in list variable in my bash script [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 3 years ago.
I'm looking at a bash script with this:
PARAMS="%1;$1;$2;$3;$4;$5;$6;$7;$8;$9"
As I understand it, the parameters that are passed in on script execution will be added to this list.
When I run this:
runscript.sh CONFIG 2>&1
I see this error:
line 76: [[%1;CONFIG;;;;;;;;: command not found
where line 76 contains this:
if [[$PARAMS =~ "CONFIG" ]];
What does the %1 mean and how should I run the script to get it to work?
I didn't have a space after the [[. Once added the script worked.

Running a command line through shell script is not working [duplicate]

This question already has answers here:
How to execute a bash command stored as a string with quotes and asterisk [duplicate]
(5 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 4 years ago.
I have an application in Unix. I use the below command to connect to it:
./application -a "connect"
I want to do the same through the shell script, for which i assigned the command line to a variable like:
newcommand = './application -a "connect"'
$newcommand
But this is not working.
However the first part of the code is working. i.e.,:
newcommand = "./application"
$newcommand
Can anyone point out what i am missing.
Believe it or not, this:
newcommand = "./application"
...has the shell run the command, newcommand with the arguments, =, and ./application.
In shell simple assignments cannot have any unprotected whitespace or they'll be interpreted as a command.
Consider:
newcommand=./application
$newcommand
...notice that there's no space around the = sign in the assignment.

Assigning value not working in a shell [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 7 years ago.
OS: Ubuntu 14.04
I created a test file called test and in it I have:
#!/bin/bash
NEW_VALUE = "/home/"
echo $NEW_VALUE
chmod +x test
./test
Produces the following:
./test: line 2: NEW_VALUE: command not found
Any ideas why this is not working?
In Bash you can't have any space around the = sign when assigning a variable.
Any space will end the assignment, even after the =, e.g.:
test_var=this is bad
#=> is: command not found
#CharlesDuffy's comment explaining why this happens
Check this link for more information on variable assignment in bash: http://wiki.bash-hackers.org/scripting/newbie_traps#setting_variables
Remove the white space while assigning then it'll work fine. just like:
#!/bin/bash
NEW_VALUE="/home/"
echo $NEW_VALUE

Resources