How to Execute Command with Parameters

Execute command in bash with parameter and return value

This should work better.

#!/bin/bash
targetserver="192.168.3.1"
commandline=$(mount | grep "$targetserver" | wc -l)

if [ $commandline -gt 0 ]; then
echo "Mounted !"
else
echo "Not mounted"
fi

You could shorten it down though, using $?, redirection and control operators.

targetserver="192.168.3.1"
mount | grep "$targetserver" > /dev/null && echo "mounted" || echo "not mounted"

Depending on system grep /etc/mtab directly might be a good idea too. Not having to execute mount would be cleaner imho.

Cheers!

exec command with parameters in Go?

exclude exit status 1 from err will get right result.

cmd := exec.Command("bash", "-c", "acme.sh --issue --dns -d exmaple.com --yes-I-know-dns-manual-mode-enough-go-ahead-please");
out, err := cmd.CombinedOutput()
if err != nil && err.Error() != "exit status 1" {
log.Fatalf("issue failed with error: %s\n", err)
}
fmt.Printf("combined out:\n%s\n", string(out))

How to execute command with parameters?

See if this works (sorry can't test it right now)

Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php", "-m", "2"});

Executing Command line .exe with parameters in C#

Wait for the process to end (let it do its work):

ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);

procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;

// wrap IDisposable into using (in order to release hProcess)
using(Process process = new Process()) {
process.StartInfo = procStartInfo;
process.Start();

// Add this: wait until process does its work
process.WaitForExit();

// and only then read the result
string result = process.StandardOutput.ReadToEnd();
Console.WriteLine(result);
}

How to execute command from the controller and pass parameters to it? LARAVEL

I would have to guess that you are passing a Collection as the second argument to call. You would have to ask that Collection for the underlying array:

Artisan::call('fidelizaleads:quotesWithoutProjects', $translateWithouthProyect->toArray());

You can use toArray() on the Collection to get the array.

Though I am not sure what passing that array is going to do as you don't have any parameters for that command and the array is usually an associative array.

Bash: Execute command WITH ARGUMENTS in new terminal

Try this:

gnome-terminal --window-with-profile=Bash -e 'bash -c "route -n; read"'

The final read prevents the window from closing after execution of the previous commands. It will close when you press a key.

If you want to experience headaches, you can try with more quote nesting:

gnome-terminal --window-with-profile=Bash \
-e 'bash -c "route -n; read -p '"'Press a key...'"'"'

(In the following examples there is no final read. Let’s suppose we fixed that in the profile.)

If you want to print an empty line and enjoy multi-level escaping too:

gnome-terminal --window-with-profile=Bash \
-e 'bash -c "printf \\\\n; route -n"'

The same, with another quoting style:

gnome-terminal --window-with-profile=Bash \
-e 'bash -c '\''printf "\n"; route -n'\'

Variables are expanded in double quotes, not single quotes, so if you want them expanded you need to ensure that the outermost quotes are double:

command='printf "\n"; route -n'
gnome-terminal --window-with-profile=Bash \
-e "bash -c '$command'"

Quoting can become really complex. When you need something more advanced that a simple couple of commands, it is advisable to write an independent shell script with all the readable, parametrized code you need, save it somewhere, say /home/user/bin/mycommand, and then invoke it simply as

gnome-terminal --window-with-profile=Bash -e /home/user/bin/mycommand

How to execute a Bash command from variable with argument

Storing a whole command line in a variable is not safe and will have many issues.

Use functions instead:

greet() { if [ 1 == 1 ]; then echo hello $1; fi; }

Then call it as:

greet world

Which will output:

hello world

(As per comments below)

With the known risks of eval, you can do something like this:

greet='if [ 1 == 1 ]; then echo hello $1; fi'
(set -- word && eval "$greet")

This will output:

hello world


Related Topics



Leave a reply



Submit