Getting script that works from terminal to work from run - expect

I am using a script which is basically identical to the first one in https://stackoverflow.com/a/4783182/7238575. It works perfectly when run in the terminal or using the option "Run in terminal" (when setting the preference to asking what to do when double clicking a script in Nautilus). However, when I just "Run" it (or when the preference is set to run) it does not work.
What is the difference and what is going on? How can I make the script work with the default "Run" (I would prefer to keep the preference to always use "Run" so that I can just double click to run the script without any dialogs.)
I tried to source bashrc by adding source ~/.bashrc to get the same environment but that made the script fail even from the terminal so I must have done something wrong. (I commented out the lines in bashrc that preven it from running in interactive mode).

Related

Run msys2 shell in VSCode terminal

I am working with (a customized install of) msys2, and I would like to make it run in the VSCode integrated terminal. However, it doesen't seem to want to run there. Unlike normal msys, it has msys2_shell.bat & msys2_shell.cmd. It seems to always open up a separate shell window, but I don't want this to always happen, I would like to keep it integrated so I can use shortcuts and such. I already tried the -no-start option. Any suggestions?
Thanks!
msys2_shell.cmd -no-start -defterm could work. Other than that, you can invoke Bash directly if you remember to set MSYSTEM and invoke a login shell: C:\msys64\usr\bin\env MSYSTEM=MSYS /usr/bin/bash -li
For more details, see https://www.msys2.org/wiki/Launchers/.

WebStorm: bash script errors when run with "run", but works when executed in the console

I wrote a build script for my project (maven, grunt as well as some server-reconfiguration), and when I use the WebStorm command-line with
~/Projects/javaee-angularjs$ ./launchServer.sh
Everything works as expected.
However, when I configure the file to be run by WebStorm (the green triangle top-right) it fails because of: /home/user/Projects/javaee-angularjs/launchServer.sh: line 29: grunt: command not found
The run-config should be absolutely correct:
working directory is the correct one and the script is found to begin with.
Any Idea what could cause this weird behavior?
P.S. I don't know whether WebStorm can run bash natively, since I've always had the bash plugin installed.
Try starting WebStorm from terminal - does the issue persist?
When being launched from desktop/System menu, WebStorm only sees environment variables configured in ~/.profile (login shell), but not in interactive shell configuration files (like ~/.bashhrc). Possible workarounds:
Workaround 1: make required variables available in a login shell (i.e. for bash, move them from .bashrc to .bash_profile).
Workaround 2: run IDE from a terminal, via bin/webstorm.sh
Workaround 3: edit the desktop launcher and set command to /bin/bash -l -i -c "/path/to/webstorm.sh"
see also https://youtrack.jetbrains.com/issue/IDEABKL-7589
The error sounds like Webstorm cannot find grun in its path. Try using the full path to the grunt command.

Remove "Last login" message for new tabs in terminal

After some search about it I created a ~/.hushlogin file and it worked, but only for new windows. Is there a way to make it work for new tabs too?
On Mavericks, creating the empty file ~/.hushlogin removes the line “Last login”, for new windows and new tabs.
On my Mac it works.
Solution
This is running OS X 10.8.3. I haven't tested it on other versions, but so long as Terminal has the above option, then it should work.
In Terminal.app, go to Preferences->Settings and select the profile you're using. Go to the 'Shell' tab and under the 'Startup' heading, check 'Run command:' and enter into the box:
login -fpql your-username /bin/bash
Substitute your-username with your actual Unix username. If you use a shell other than the default bash shell, replace /bin/bash with the full path to that shell (yes, even if you've already set it in Preferences->Startup.)
Make sure 'Run inside shell' is unchecked.
If you have the "Prompt before closing: Only if there are processes other than login shell and:" option selected, add "login" and "bash" to the list of processes it will not prompt for.
Make sure you have a ~/.bashrc file, since this will be the file bash uses on startup from now on rather than ~/.bash_profile. I just have one file reference the other using this method. You also need to be sure it sources /etc/profile.
Explanation
We want to run login with the -q option to tell it to supress the "Last login" message, even in the absence of a .hushlogin file. (As noted above, login will only look in cwd for that file, not your home directory, so you'd need a .hushlogin file in every directory you'd open a shell to, for that method to work.)
The problem is Terminal runs something like login -pfl your-username /bin/bash -c exec -la bash /usr/local/bin/bash when you create a new shell (I'm using homebrew's version of bash, hence the weird bash path at the end,) which lacks the -q flag.
Unfortunately, there's no way to directly change the arguments Terminal uses, so we just trampoline a new login session with login -pfql from Terminal's default login -pfl session. Inelegant, but it works.
We need to have the -q option and the path to bash to keep the "New windows/tabs open with: Same Working Directory" option working. If you don't care about that option, you can remove that flag and argument, and probably avoid the .bashrc stuff above.
you could just add a clear to your .bash_profile
Adding ~/.hushlogin is fine unless you want to open a new tab in the same folder, or open Terminal from Finder on the exact folder, in that case it won't work.
Changing a running command to another login is something I would like to avoid because of the strange unnecessary scheme login -> login -> zsh. You can see it in Activity Monitor, but also it will show up when you are quitting interactive programs (like, python repl) in the message that python, login and zsh are running.
Putting clear in ~/.zshrc is not ideal since on mac it just prints a lot of newlines (and if you scroll back, you'll see them).
The best way that I found up to this point is adding printf '\33c\e[3J' to ~/.zshrc (or in Terminal/Preferences/Profiles/Shell/Startup/Run command with Run inside shell checked). I chose beginning of ~/.zshrc file since startup command is running after it and if the ~/.zshrc file is heavy you can briefly see Last Login message before printf is executed.
This might be OS version dependent. On Terminal 2.3 (on 10.8), touching the file ~/.hushlogin suppresses the 'last login' message for new tabs as well as new windows. That is, it Works For Me.
Just in case it helps to work out what's going on (and in case you don't know), note that the 'last login' message is in principle coming from login(1), and not the shell. Or, more precisely, if a shell is invoked in a particular way (including starting it with the -l option), then bash will "act as if it had been invoked as a login shell" (zsh may have a similar feature, though I can't find it right now). Now, it could be that when Terminanl opens up a new tab in your OS X version, the shell is effectively simulating opening a login shell, and maybe getting this detail wrong. But if you have the 10.8 version of bash/zsh (namely 3.2.48 / 4.3.11), then I don't know what might be amiss.
A simple solution without changing anything related to login would be just to add the clear command in the end of your ~/.bashrc or ~/.zshrc file. It will clear the terminal in initialization from any initialization prints. It works for me very well.
On my MacOS Big Sur 11.1 it works.

In Bash, how can I tell if I am currently in a terminal

I want to create my own personal logfile that logs not only when I log in and out, but also when I lock/unlock my screen. Kindof like /var/log/wtmp on steroids.
To do this, I decided to run a script when I log into Ubuntu that runs in the background until I quit. My plan to do this is to add the script to .bashrc, using ./startlogging.sh & and in the script I will use trap to catch signals. That's great, except .bashrc gets run every time I open a new terminal, which is not what I want for the logger.
Is there a way to tell in Bash that the current login is a gnome login? Alternatively, is there some sort of .gnomerc I can use to run my script?
Edit: Here is my script:
Edit 2: Removed the script, since it's not related to the question. I will repost my other question, rather than repurpose this one.
Are you looking for a way to detect what type of terminal it is?
Try:
echo $TERM
From Wikipedia:
TERM (Unix-like) - specifies the type of computer terminal or terminal
emulator being used (e.g., vt100 or dumb).
See also: List of Terminal Emulators
for bash use : ~/.bash_logout
that will get executed when you logout, which sounds like what you are trying to do.
Well, for just bash, what you want are .bash_login/.bash_logout in your home directory (rather than .bashrc) These are run whenever a LOGIN shell starts/finishes, which happens any time you log in to a shell (on a tty or console, or via ssh or other network login). These are NOT run for bash processes created to run in terminal windows that you create (as those are not login shells) so won't get run any time you open a new terminal.
The problem is that if you log in with some mechanism that does not involve a terminal (such as gdm running on the console to start a gnome or kde or unity session), then there's no login shell so .bash_login/logout never get run. For that case, the easiest is probably to put something in your .xsessionrc, which will get run every time you start an X session (which happens for any of those GUI environments, regardless of which one you run). Unfortunately, there's no standard script that runs when an X session finishes.

Running shell commands from Xcode breakpoint

I'm trying to run a shell script from a breakpoint in Xcode 4.5 DP 3. I set the breakpoint as such using the Choose button.
However, upon hitting the breakpoint I get the following message:
Error in shell command for breakpoint "(selector name)". The command "/Users/Max/Developer/saveToLog.sh" does not exist.
I've tried to put regular shell commands, like "say test" but Xcode prints the same message (with a different command name, of course). The shell script works fine when I run it from terminal.
Ok, first check to make sure you have the shell permissions set correctly. In terminal type:
chmod u+x saveToLog.sh
And I would also locate the shell script inside the project folder (didn't seem to work correctly for me when the script was located on my desktop).
This is the script I tested on:
#!/bin/bash
touch ~/Desktop/ItWorks.txt
echo "This actually works!" > ~/Desktop/ItWorks.txt

Resources