I tried to run shell script in new terminal window, from mac osx. I use open cmd like this and it works fine
open -a Terminal script.sh
My issue is I have to pass arguments in my shell. I tried
open -a Terminal script.sh arg
open -a Terminal script.sh --args arg
open -a Terminal --args script.sh arg
open -a Terminal "script.sh arg"
But nothing is working !!!
Do you know a way to do that?
I found something. I don't like this but it works:
osascript -e 'tell application "Terminal" to do script "cd ${Dir} && ./script.sh ${arg}"'
Have you tried sudo bash /path/to/script -option1 -option2(if your script needs sudo) or bash /path/to/script -option1 -option2(if no sudo is needed) from the terminal?
Related
The following bash script is suppose to open 2 new terminal tabs then execute respective commands:
mate-terminal --tab -e "cd ~/ece344/root; sys161 -w kernel" --tab -e "cd ~/ece344/root; cs161-gdb kernel"
The script does open 2 new tabs however both tabs display the following error:
There was an error creating the child process for this terminal
Failed to execute child process "cd" (No such file or directory)
Ps. The answer should work with mate-terminal.
I don't have mate installed but I would try :
mate-terminal --tab -e "/bin/bash -c 'cd ~/ece344/root; sys161 -w kernel'" --tab -e "/bin/bash -c 'cd ~/ece344/root; cs161-gdb kernel'"
The idea is that "-e" would want to execute a command that probably run inside the window instead of a default shell, so from the error I understand that "cd" is not a real program in an expected location (since 'cd' is in the PATH shouldn't be a problem.
So my example would provide a full path to a shell "/bin/bash" that would then execute the commands you want.
I am opening a new screen in detached mode from the following one-liner bash script:
screen -d -m -S screenName
But the terminal prompt of screen displays bash-3.2$ instead of my regular prompt atul.vaibhav#Mac$.
When I execute the same command directly from terminal, I get normal expected prompt. Why am I getting a different prompt when opening a screen from bash script?
I am executing my bash script in this manner:
./myScript.sh
Since the synopsis from man screen gives:
screen [ -options ] [ cmd [ args ] ]
and you want a login Bash shell, you should probably run:
screen -d -m -S screenName -- bash -l
That said, while the command ran and ps claimed to have created a bash -l somewhere, I couldn't see the window.
Try this:
bash -l ./myScript.sh
or
bash -i ./myScript.sh
From man bash:
-l: Make bash act as if it had been invoked as a login shell.
-i: If the -i option is present, the shell is interactive.
Is there a way to pass arguments to a program being run via:
open -a /Applications/Utilities/Terminal.app ~/my_executable
I have tried:
open -a /Applications/Utilities/Terminal.app ~/my_executable arg1 arg2
But this is interpreted as telling the terminal to open ~/my_executable ~/arg1 ~/arg2.
I have tried:
open -a /Applications/Utilities/Terminal.app '~/my_executable arg1 arg2'
But it picks up arg1 and arg2 as if they were part of the path rather than arguments.
I have tried:
open -a /Applications/Utilities/Terminal.app ~/my_executable | xargs arg1 arg2
I have also tried:
open -a /Applications/Utilities/Terminal.app ~/my_executable --args arg1 arg2
But with that flag, args are passed to the terminal.
NOTE
I am only allowed to change the arguments to Terminal.app (the part within [ ]):
open -a /Applications/Utilities/Terminal.app [~/my_executable arg1 arg2]
Edit: Leaving the original answer below as some people seem to find it useful, but keep in mind that this doesn't really answers OP's question, this is to pass arguments to an app opened with "open" not to and app opened with Terminal.app which was opened with "open".
You can find your answer by running open without arguments:
% open Usage: open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-b <bundle identifier>] [-a <application>] [filenames] [--args arguments]
[...]
--args All remaining arguments are passed in argv to the application's main() function instead of opened.
[...]
You can see there is an option --args you can use it like this:
open ./Untitled.app --args arg1 arg2 arg3
I tested it on el Capitan (10.11.3) so I don't know if the option is present in earlier versions.
Probably the easiest way is to create a temporary shell script, e.g.
$ echo "~/my_executable arg1 arg2" > /tmp/tmp.sh ; chmod +x /tmp/tmp.sh ; open -a Terminal /tmp/tmp.sh ; rm /tmp/tmp.sh
Yes, I know. need to manage another script.
but think differently. you work not on Terminal, but on Script Editor.
(not bash scripting, but AppleScript'ing)
property testScript : "/tmp/sh.sh"
set input to display dialog "args?" default answer ""
log input
tell application "Terminal"
activate
do script testScript & " " & text returned of input
end tell
For those with having an issue with Paul R's answer, add the ; rm /tmp/tmp.sh to the tmp.sh script itself. Don't add a sleep command like Maksim as that creates a race condition.
echo "~/my_executable arg1 arg2 ; rm /tmp/tmp.sh" > /tmp/tmp.sh ; chmod +x /tmp/tmp.sh ; open -a Terminal /tmp/tmp.sh
Use the --args switch before your arguments.
open ./Terminal.app --args --your-args
From the docs:
--args: All remaining arguments are passed in argv to the application's main() function instead of opened.
I need to open a new terminal tab and execute multiple commands on it, how can I do it. I have already tried the following,
gnome-terminal --tab -t "X" -e "cd ~/Desktop/terminal_test;mkdir test"
Here I need to cd into a new directory and create a new folder.
Try this:
gnome-terminal -x bash -c "cmd1; cmd2; …cmdN; exec bash"
You can use gnome-terminal. The script below will open 3 tabs running the respective commands..
tab="--tab"
cmd01="bash -c 'ls';'pwd';bash"
foo=""
foo+=($tab -e "$cmd01")
gnome-terminal "${foo[#]}"
exit 0
I already know how to open a terminal in a bash with gnome-terminal and execute a program:
gnome-terminal -e ./OpenBTSCLI
But I also need that once open that program in the new terminal, write another command inside.
When a I tried to use echo, the message appear in the terminal where I run the bash.
I tried: gnome-terminal -e "bash -c './OpenBTSCLI && echo message'" that I find online but its not working, it only do the first part.
Anyone have an idea of how to resolve this? Thank you
I think it does the second command as well, but the new terminal closes as soon as the command's finished, so you don't see it. I reversed the order of quotes and added a 1s sleep at the end to allow seeing the echo.
gnome-terminal -e 'bash -c "./OpenBTSCLI && echo message && sleep 1"'