This question already has answers here:
Script parameters in Bash
(5 answers)
Closed 6 years ago.
I've written a simple bash script, named ocropus:
#!/bin/bash
read filename path
...
And then i realized that I can't run it like this:
ocropus filename path
Instead, I need to run it like this:
ocropus
filename path
What can I do so I don't need to hit enter before my inputs? Thanks a lot!
Command line arguments are in $1, $2, etc. So do:
filename=$1
path=$2
instead of
read filename path
Related
This question already has answers here:
Make a Bash alias that takes a parameter?
(24 answers)
Closed 8 months ago.
How to make alias that take argument and interpolate it to file extension to create it? I tired this and this doesn't work :
alias create-bash-file=' echo "#!/bin/bash" > "$1.sh" '
as $1 is first argument and .sh is file extension and I wanted to make an alias that create a bash file included "#!/bin/bash" but the file name I want to take it as an argument so for example and I will write create-bash-file filename this should generate bash file called filename.sh
Aliases don't take arguments; functions do.
create-bash-file () {
echo "#!/bin/bash" > "$1.sh"
}
This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Closed 1 year ago.
So I'm trying to compose a string using script shell
#!/bin/sh
blNumber=1111619832
echo '*'$blNumber+='*.xlsx'
I expect the output to be: *1111619832*.xlsx
but as a result I get: *+=*.xlsx
Btw I tried to figure out why so I tried this code
#!/bin/sh
blNumber=1111619832
output="*${blNumber}"
and whenever I add something after *${blNumber} it get concatenated at the begging of the string
Why are you using += in the first place?
$ echo '*'$blNumber'*.xlsx'
*1111619832*.xlsx
Or put it inside double-quotes. It's best practice to quote all variables anyway.
$ echo "*$blNumber*.xlsx"
*1111619832*.xlsx
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.
This question already has answers here:
How to read a file into a variable in shell?
(9 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
I have store ip address with port in a file and I want to read it using shell script. Thus file serverIP has data 192.168.1.17:3000. I am using following bash script to read it
IPAddressFile=/home/geo/serverIP
SERVER_IP_PORT=$(<$IPAddressFile)
echo $SERVER_IP_PORT
But this script echo empty string. Where I am making mistake?
If you're going to use bash-only syntax like $(<...), your script must be run with bash, not sh.
Thus, either run bash yourscript or add a #!/bin/bash (or similar) shebang, flag the file executable, and invoke it as a command, for example ./yourscript
As an alternative that's both efficient and compatible with POSIX sh:
IFS= read -r SERVER_IP_PORT <"$IPAddressFile"
This question already has answers here:
Shell command to retrieve specific value using pattern
(3 answers)
Closed 8 years ago.
I have file test.txt contains the following
AA=testing
BB=help
CC=hello
How can i make a bash script that will get each value and assign to a new variable?
#!/bin/bash
var1=testing
var2=help
var3=hello
thanks for the help
First of all a = value is not correct syntax in shell. In shell the spaces are important.
When you have a valid file, you can use the eval function to evaluate that file as a string, or simply source it.