Very simple bash script not working - bash

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'

Related

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

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.

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.

applescript does not find command line tool under /usr/local/bin

AppleScript does not find a command line tool located in /usr/local/bin/.
I've got the following AppleScript command:
do shell script "/usr/local/bin/bitbar refresh"
It results in the error:
error "env: node: No such file or directory" number 127
I checked the directory and the command line tool is indeed in that location. AppleScript does find other tolls installed there, such as brew.
Any ideas?
Try initially adding a new path (i.e. /usr/local/bin/) to the shells PATH variable before running the bitbar refresh command.
For instance, change your do shell script as follows:
do shell script "PATH=/usr/local/bin:$PATH; bitbar refresh"

navigate through folders using shell script

I am new to shell scripting. I have saved the script file as script_hdl in my home directory. From my home directory, I want to navigate using the script in the following order: cd ../../site/edu/ess/project/user/rark444
and then open a new tab from this new location in the terminal.
I used this as my script:
#!/bin/bash
alias script_hdl="cd ../../site/edu/ess/project/user/rark444"
I run the script like this
./script_hdl
But I don't see any response in the terminal. I feel I am missing something but I don't know what is it. Thanks in advance for your help.
You have two ways to change directory here.
Script
The first one is to write a script, in such a way that you can run other command after cd. It works without the alias command: let's say you remove it.
cd command is proper to the running process. When you execute your script, the following happen:
your shell spawns (forks as) a new shell process executing your code. The main process wait for its child to finish;
this new child process actually does change its own working directory with your cd command, then quits (it's over)
the original shell process stops waiting and prints the prompt again. But this process has not changed directory (only the child process did)
To perform what you want, (remove the alias command, then) call your script as follows:
source script_hdl
or with following shortcut:
. script_hdl
meaning that you want the instructions to run in the same shell process.
Alias
The second way to change directory is to use an alias. But you should not write your alias definition in a random script file, add it in your ~/.bashrc instead (this file is run each time you open a shell).
So:
alias script_hdl="cd ../../site/edu/ess/project/user/rark444"
to reload ~/.bashrc:
. ~/.bashrc
And then don't try to execute from the file, just launch your alias as if it was a normal command:
script_hdl
Looks like you are trying to set up an alias. You can do this by editing your .bash_profile file in your home directory (if it's not there you can create one and then run "source .bash_profile" after editing it) and make an entry like alias script_hdl='cd ../../site/edu/ess/project/user/rark444' and then run "script_hdl" from your terminal.
For more info on alias you can follow the link mentioned by Paul.
Make sure the spelling is correct as unix is case sensitive and that you have permissions. First try it on the command line to ensure that it works, if there is an error it will appear on the command line as sometimes scripts hide the errors and messages. If it works then copy the text to the script file and don't use alias.
Here is the correct usage of alias
https://en.wikipedia.org/wiki/Alias_(command)

cywin bash script command not found when called from batch

#!/bin/bash
echo "Testing"
cd "/cygdrive/x/Internal Communications/Riccardo/"
filename=htdocs-`date +%A`.tar.gz
tar cvzf $filename "/cygdrive/c/Program Files/Zend/Apache2/htdocs"
The above script is working when it is called inside cygwin console, but when I try to call it from a batch file I get "command not found" for date and tar command. I think that bash.exe does not have the PATH set up.
I need to run that script from that batch file because I want to add the script to the task scheduler.
As has already been said, you need to add the Cygwin binaries to your path. To do so, right click on "My Computer", click "Properties", then "Advanced", then "Environment Variables".
Create a new environment variable with name "CYGWIN_HOME" and value "C:\cygwin" (or wherever you installed cygwin. The default location is "C:\cygwin\" so this should probably work for you).
Then edit the environment variable named "PATH", and tack on the following to the end:
;%CYGWIN_HOME%\bin;%CYGWIN_HOME%\sbin;%CYGWIN_HOME%\usr\bin;%CYGWIN_HOME%\usr\sbin;%CYGWIN_HOME%\usr\local\bin;%CYGWIN_HOME%\usr\local\sbin
Close your command prompt, then reopen it. The cygwin binaries should now be available. You can double-check this by typing "which bash". It should report the location of your bash executable.
FWIW, Cygwin has cron.
Are you calling your script like this?
bash --login -i ./myscript.sh
Put your cygwin bin directory (likely C:\cygwin\bin) on your PATH environment variable.
This would also give you the benefit of being able to use commands like tar, ls, rm, etc. from a regular console windows and not just a Cygwin console.
If this script is invoked from a Windows command shell, the first line will result in an error since #!/bin/bash is not a recognized Windows command and # is not a valid comment delimiter in a batch file.
So, the bottom line is that this script runs as a regular batch file rather than from within Cygwin's bash. As noted by matt b, you likely do not have the Cygwin executable path in your PATH environment variable. Without this, the batch file cannot find the Cygwin utilities (tar and date).
I just had this problem.
Editing the environment variable works great. But if you are have no admin rights you can´t do that.
In this case you can execute your commands by using the absolute path like:
/usr/bin/tar cvzf $filename
/usr/bin/cat $filename
If you do so your bash script works even if you call it from a batch file.

Resources