This question already has an answer here:
How to pass output as command line argument in bash? [duplicate]
(1 answer)
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Im not sure if it is possible, but would like to know if i can use the result of a main() function as the command line argument for another program. im currently using gcc compiler on linux so i figure it would look something like this:
./listcommandlinearugments ./generaterandomstring
but this does not work... in the above example the first program (listcommandlinearguments) simply lists ./generaterandomstring as a command line arugment instead of using the result of the main funtion (0) because it was int main() and returned 0.
You can use backticks to insert the output of a program into another command:
./generaterandomstring `./listcommandlinearugments`
EDIT:
Since you want the return value of the listcommandlinearugments, run that first, then use $? to get the return value:
./generaterandomstring
./listcommandlinearugments $?
Related
This question already has answers here:
Difference between ${} and $() in Bash [duplicate]
(3 answers)
Brackets ${}, $(), $[] difference and usage in bash
(1 answer)
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
In the bash shell,
$ date +%Y%m%d%H%M
202208161535
So I put it in a bash script
#!/bin/bash
dat=${date +%Y%m%d%H%M}
echo "dat=" ${dat}
But when I run it, I get
$ test.bash
./test.bash: line 2: ${date +%Y%m%d%H%M}: bad substitution
dat=
How should I do it?
ADD
I found
dat=`date +%Y%m%d%H%M`
works. But I'm curious how I can do it with dat = ${data +%Y%m%d%H%M}.
ADD2
This question arose because of the mistake, or rather from not knowing the difference of ( ) and { }. Those who cannot notice this difference cannot search with search pattern 'difference of ( ) and { } in bash'. So the referenced links 'supposed to have solution for this question' cannot be searchable by the people like me. So I think this question is worth being kept as is.
#!/bin/bash
dat=`date +%Y%m%d%H%M`
echo "dat="${dat}
The above code is working code
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.
This question already has answers here:
How to call shell commands from Ruby
(22 answers)
Closed 8 years ago.
Is there any way to use command line commands in ruby code ?
Like : Some third party .ipa installer command inside ruby code(reinstall the app between scenarios using a 3rd party installer like ideviceinstaller).
Kernel#exec, that replaces your ruby process with the one you specified, as a corresponding syscall. Therefore, it ends the program even if there's more code to run. Probably not what you want. Works like: exec("this")
Backticks. `this` will run this and return its stdout as a string. The same thing with different syntax: %x(this)
Kernel#system: mostly same as exec, but doesn't replace your Ruby process and returns a boolean... most of the time: whether it worked successfully (true), it returned non-zero (false) or failed to run at all (nil); runnable as system("this")
See these three and links to more
This question already has answers here:
Using getopts to process long and short command line options
(32 answers)
Closed 8 years ago.
I want to write a bash shell script, it can accept parameters, and the parameter has prompt, example,
./test.sh --version=1.0
value 1.0 is the real parameter for my shell, and --version= is the prompt
is there any easy way to do it like this?
You should have have a look at man(1) getopt.
Depending what you mean by "easy" - using getopts should work. A bit of typing, but... Here are some examples to get you started:
http://rsalveti.wordpress.com/2007/04/03/bash-parsing-arguments-with-getopts/
How do I parse command line arguments in Bash?
http://spin.atomicobject.com/2011/03/30/parsing-arguments-in-bash-with-getopts/
This question already has answers here:
How do I get the application exit code from a Windows command line?
(7 answers)
Closed 9 years ago.
print exit code in cmd in windows os ....some command return exit code ...where this code store...and i want to print this exit code and assign it to variable
You can use %ERRORLEVEL% environment variable, it will hold the last code that was returned. You can echo it or assign it to a variable, depending on your needs.