Ubuntu desktop entry based on bash script: How to access external files - bash

I am learning bash and am working on a bash script that allows me to select a keyboard led-profile (keyboard-color-picker.sh).
The led-profiles are defined in external files (e.g. my-favorite-color-profile), placed in the same folder as the bash script itself.
Such file is accessed like this in the script:
g213-led -p my-favorite-color-profile;;
When I run the bash script from terminal inside the parent folder, everything works as expected.
As I wanted to run my bash script via launcher, I created a desktop file
~/.local/share/applications/color-picker.desktop, where the exec line points to my script:
Exec=/home/me/bin/keyboard-color-picker/keyboard-color-picker.sh
Now my problem is, when envoked via launcher, the script does not seem to access the external files anymore. The profile defined in e.g. "my-favorite-color-profile" is not loaded.
What do I need to change so that my bash script runs via launcher the same way as it runs via terminal from the folder.

The underlying question is how to cd into the absolute path of my script, which is answered, e.g., here.

Related

Running shell script on Windows via Cygwin mintty.exe doesn't execute runcoms

Problem summary
I'm trying to launch a .sh script via Windows 10 Cygwin (i.e. mintty.exe > bash.exe) but neither .profile, .bash_profile, or .bashrc are loading, which I need to update PATH env variable with Cygwin's bin directory.
Background
I'm trying to launch a script finder.sh:
#!/bin/bash
find .
read
from C:\Users\Bo\Temp\. It has unix line endings and executable bit set.
I have Cygwin installed at C:\Users\Bo\AppData\Local\Programs\cygwin64\. I do not have this path in either System or User Windows' Environment Variables (and I don't want to!). My runcoms all live in this directory under /home/Bo. My .bash_profile (and ATM .bashrc) have an export PATH="/cygdrive/c/Users/Bo/AppData/Local/Programs/cygwin64/bin":${PATH} in them.
I want to launch the script from Windows Explorer. I tried using the bash.exe and mintty.exe in the cygwin64\bin\ folder via Open > Choose another app > More apps > Look for another app on this PC. In either case the mintty window displays:
FIND: Parameter format not correct
meaning the Windows' find command was used not Cygwin's. So I have my script echo $PATH and the Cygwin/bin directory is not in PATH. If I add the proper export PATH statement from above to my own script it works fine. So, now to debug the launcher and runcoms...
I've put echo ${0} statements in .profile, .bash_profile, and .bashrc, none of which trigger which I run the .sh script, they are never run. I've read SO and the mans. I've tried creating a Shortcut to both mintty.exe and bash.exe passing a variety of -l -i -e - commands to each using Properties > Shortcut > Target and they are never run. E.g. running simply [..]\mintty.exe -h always doesn't even leave the window open.
How do I get my script to run in Windows Explorer via Cygwin's mintty.exe/bash.exe, and to read from a runcom to update PATH (to find Cygwin Linux commands, vs. updating Windows Environment Variable)?
Two part fix:
A) set a Windows Environment Variable for BASH_ENV to a .bash_env under your Cygwin HOME, and export the PATH variable to include the Cygwin/bin directory from that file. I cannot find a decent reference for this in Cygwin documentation because it seems to be simply a bash thing, but this variable is what bash looks for when running non login/interactively. Best reference: Cygwin shell doesn't execute .bashrc.
And B) run the .sh with bash.exe from Cygwin/bin using Open With....
ALSO, annoying Windows bug: when you select a program to Open With... your .sh script, it will always run 1x with a CWD from your C:\Windows\System32 directory(?!) and all other times will run fine with the CWD as the directory from your .sh script.

Ubuntu Linx, how to run a shell script with a specific users PATH, without having to change .bashrc file?

I want to dynamically create a user account from a shell script (which runs while my device boots up) and run some more shell scripts under the new user's account. Right now I am using the below command to run the shell scripts under a specific user account.
su -c "eval $shellscript.sh" -s /bin/sh **newuser**
This is working to the extent that, shell scripts are actually running under the user name newuser. However, I am facing another problem.
The newuser's PATH variable is initialized with /usr/bin:/bin: However shellscript.sh uses some commands (like ifconfig etc.,) which are not present in the above folders. Hence these commands are failing.
I am trying to figure out how to add the required folders to the PATH variable of "newuser" without modifying the file ./~profile of newuser, and also while making sure that PATH gets updated only for this user but not for the entire system.
In a nutshell I want to update the PATH variable of newuser dynamically from a shell script and make some other shellscript use this PATH variable.
is there any such possibility in Ubuntu Linux?

How to Open a Git Bash Shell inside Box Drive folder

I am attempting to open a bash shell inside my Box folder. However, I get the following error:
"This file does not have an app associated with it for performing this action. Please install an app or, if one is already installed, create an association in the Default Apps Settings page.
When I right click and select 'Git Bash Here' in Dropbox, however, the Bash shell opens successfully.
I know you can associate a WSL bash shell to .sh files (on Windows 1, with the WSL feature activated)
You could adapt that script in order to not call "%SYSTEMROOT%\System32\bash.exe" (the WSL bash) but C:\path\to\git\bash.exe
But first, check if the issue persists outside a dropbox folder.

Run a remote bash script on a Mac using PuTTy

I want to run a bash script on a mac remotely from a batch script on a windows machine. On Windows I have this:
#echo off
echo bash /applications/snowflake/table-updater/test2.sh; exit>tmp_file
putty -ssh User#remote_machine -pw password -m tmp_file
And here is test2.sh on the remote machine
#!/bin/bash
# test2.sh
#
#
7za x table-apps.zip -y -o/Applications/snowflake/applications
When the batch file runs it logs in successfully, but for some reason fails to run the bash file. However the bash file runs fine from mac terminal, where it unzips the files perfectly. What could be happening here?
Please note test2.sh is actually in Applications/snowflake/table-updater as specified in the batch file. And the tmp file does write fine as well. My aim is to have a script to access a further 10 remote machines with the same directory structure.
Thanks in advance
The standard program which resembles the scriptable Unix command ssh in the PuTTy suite is called plink, and would probably be the recommended tool here. The putty program adds a substantial terminal emulation layer which is unnecessary for noninteractive scripting (drawing of terminal windows, managing their layout, cursor addressing, fonts, etc) and it lacks the simple feature to specify a command directly as an argument.
plink user#remote_machine -pw password /Applications/snowflake/table-updater/test2.sh
From your comments, it appears that the problem is actually in your script, not in how you are connecting. If you get 7za: command not found your script is being executed successfully, but fails because of a PATH problem.
At the prompt, any command you execute will receive a copy of your interactive environment. A self-sufficient script should take care to set up the environment for itself if it needs resources from non-standard locations. In your case, I would add the following before the 7za invocation:
PATH=$PATH:/Applications/snowflake/table-updater
which augments the standard PATH with the location where you apparently have 7za installed. (Any standard installation will take precedence, because we are adding the nonstandard directory at the end of the PATH -- add in front if you want the opposite behavior.)
In the general case, if there are other settings in your interactive .bashrc (or similar shell startup file) which needs to be set up in order for the script to work, the script needs to set this up one way or another. For troubleshooting, a quick and dirty fix is to add . /Users/you/.bashrc at the top of the script (where /Users/you should obviously be replaced with the real path to your home directory); but for proper operation, the script itself should contain the code it needs, and mustn't depend on an individual user's personal settings file (which could change without notice anyway).

Very simple bash script not working

I'm making a series of bash scripts to remove to nuisance from manually typing out navigation commands into my cygwin terminal. He's one that navigates to my xampp /www/ directory:
#!/bin/bash
cd /cygdrive/c/xampp/htdocs/www
When I run it with the following command:
$ ./www.bat
I get the following error:
C:\Users\user>cd /cygdrive/c/xampp/htdocs/www
The system cannot find the path specified.
What's strange is that when I type that command out manually it navigates to the intended directory without issue. My first thought is that it's an issue with Cygwin's disk drive naming, but if that was an issue it would fail upon manual typing.
What gives?
The error you're getting is from the windows command line interpreter. It gets called because your script has the .bat extension. It should be called www.sh instead.
However, you can't do what you want with a script: a new process would be spawned to run your script, the new process would cd to your directory but at the end of the script the process would end and you'd be returned to the calling shell's process which would have the old current directory. You would need to source the script from bash (. /path/to/www.sh) so that it would run in the same process as the calling shell, but that would be overkill for what you want. Just add this to your .bashrc in your home directory (/home/<user>/.bashrc):
alias www='cd /cygdrive/c/xampp/htdocs/www'

Resources