Script shell to automate deployment site - bash

I'm trying to write a script shell (Mac OS X) for deploying my Awestruct site within a launch agent.
So, I have to generate site before deploying it. The command to generate site is :
awestruct -g
My script is myscript.sh :
cd /my/site/structure/base/directory
awestruct -g
This script is launched by the system when an event occurs.
But the problem is it doesn't know about awestruct...
My awestruct program is a part of my ruby installation and is added to my PATH variable.
When I run the script manually, it works (because it's my user who is lauynching it)
When the system detect the event and runs the script, it results as :
awestruct: command not found
The problem is the PATH...

If it works manually, then in the same prompt where it works, run command:
which awestruct
That will print the program file with full path, let's assume /usr/local/bin/awestruct, but use whatever it really is. Then use that in your script, for example:
cd /my/site/structure/base/directory
/usr/local/bin/awestruct -g
This assumes that there are no other environment variables, only defined for your account, which awestruct needs. If there are, then find out what they are, and add them to your script before running awestruct, for example with line:
export AWESTRUCT_ENVIRONMENT_VARIABLE=foobar
(Note: When you run the script normally like any program, that will not change the parent shell environment.)

You can also add the path to the executable in the PATH of the user that run the script and gets the error.
You could try something like :
$ su - <user_that_run_the_script>
$ echo "export PATH=$PATH:$(which awestruct)" >> ~/.bash_profile
$ source ~/.bash_profile
(For Linux users, use ~/.bashrc instead of ~/.bash_profile)

Related

When program uses Command to open Bash to run a script, Bash closes immediately without running the script

I have a simple, already-working bash script set up to launch specific files with specific programs in the gaming frontend EmulationStation on Windows.
But the frontend routes its actions through a Command Prompt. And when Command is used to run the script through Bash, the Bash shell just opens and then closes immediately.
Here's an image of what shows for the instant before Bash closes.
This is only happening when going through a separate Command Prompt first, such as Windows Command Prompt or Git Command Prompt. Running the script with an appropriate argument directly through the git-bash shell works just fine.
In case you want to see the script for any reason, here it is:
#!/bin/bash
defaultemulaunch="V:/Emulation/.emulationstation/systems/retroarch/retroarch.exe -L "V:/Emulation/.emulationstation/systems/retroarch/cores/bsnes_mercury_accuracy_libretro.dll" \"$1\""
emu1names=(\
"(1999) Fire Emblem - Thracia 776.smc")
emu1launch="V:/Emulation/.emulationstation/systems/retroarch/retroarch.exe -L "V:/Emulation/.emulationstation/systems/retroarch/cores/snes9x_libretro.dll" \"$1\""
gamename=`basename "$1"`
for index in ${!emu1names[*]}
do
game=${emu1names[index]}
if [ "$game" == "$gamename" ]; then
eval "$emu1launch"
fi
done
eval "$defaultemulaunch"
But it's worth pointing out that this is happening when trying to run any bash script when starting the process from a separate Command Prompt.
Note: Git is installed on the hard drive that houses the emulation frontend (V:)---not in the user directory or programs directory of the system's OS/boot drive (C:). I mention this because git-bash's failure at an apparent "login" step except when launched directly feels like it could be a default filepath issue.
Check if that program would still open/close a Windows when executed from the CMD with:
bash -c '/v/path/to/bash/script'
In your case:
set PATH=V:\Emulation\
set GIT_HOME=V:\Emulation\Git
set PATH=%GIT_HOME%;%GIT_HOME%\bin;%GIT_HOME%\usr\bin;%GIT_HOME%\mingw64\bin;%PATH
Then:
cd V:/Emulation/.emulationstation/roms/snes/
bash -c './gamelaunch.sh "./(1990) F-Zero.sfc"'
I usually make a run.bat script which would:
set the correct PATH
launch the correct script
That way, for any of my project, I just type run.
And it runs.

Running Grunt from OSX Automator shell script

I have a simple Gruntfile that I want to be able to run from an icon in my OSX dock.
I have created a very simple shell script (launcher.sh) that I will save as an application so I can add it to my dock. It works great when I run it directly in my Terminal:
#!/usr/bin/env bash
$(grunt serve --open)
It also works fine with this shebang: #!/bin/bash
However when I call the shell script from an Automator workflow I get the following error:
launcher.sh: line 2: grunt: command not found
This is my Automator set up:
What am I doing wrong?
Update
If I put this in my launcher.sh file:
#!/bin/bash
`/usr/local/bin/grunt serve --open`
I get a different error in Automator: env: node: No such file or directory
But, as before, if I run the script directly in Terminal its fine - so I think #mklement0 is right about the PATH
Update 2
launcher.sh
#!/bin/bash
grunt serve --open
Automator
PATH="/usr/local/bin:$PATH"
~/Public/Sites/launcher.sh
Now I'm still getting an error popup when I run it in Automator, but it has no details - it just says:
The action "Run Shell Script" encountered an error.
The Log panel shows a blank entry. Is there a way to get more info? A verbose mode perhaps?
Update 3
So this is weird... if I use &> ~/log it works. Without it it fails.
But this is working, thanks #mklement0, it'll do for now
PATH="/usr/local/bin:$PATH"
cd ~/Public/Sites && ./launcher.sh &> ~/log
The problem is that the $PATH variable when running from Automator doesn't have the same entries as when running from Terminal.
Notably, /usr/local/bin is missing, which is where grunt is typically installed (if installed globally).
A simple workaround is to add the folder in which grunt is installed to your $PATH at the top of the Automator shell script:
PATH="/usr/local/bin:$PATH"
~/Public/Sites/Launcher.sh
Aside from that:
Your shell command, $(grunt serve --open), should be just grunt serve --open - no need for a command substitution ($(...) or `...`), as that would actually first execute the command and then try to execute the output from that command.
The default working dir. when running a shell script from Automator is ~ (your home folder), which may not be what your script expects; it looks like your script expects its own dir. to be the working dir., so use cd ~/Public/Sites && ./launcher.sh to invoke it.
Automator will report an error in case the shell script exits with a nonzero exit code; the error message will include the shell script's stderr output (and nothing else) - sounds like no stderr output is being produced in your case.
To capture all output for diagnostic purposes, use something like ./launcher.sh &> ~/log
On macOS 10.11 through at least 10.15 (as of this update), $PATH has the following value inside a shell script run from an Automator workflow: /usr/bin:/bin:/usr/sbin:/sbin

Simple Bash Script says Command not found

I'm trying to run an extremely simple bash script that runs some commands on the shell. For now, all it is composed of is:
#!/bin/bash
eos
Where "eos" is a perfectly legit command that runs perfectly fine on the server I'm using if I run it manually on the same shell.
However, when I chmod +x and execute this script, I get the error:
./cp.sh: line 21: eos: command not found
Does anyone have any idea why it won't submit these commands?
Thanks in advance!
What user is running the script? I suspect an environment or pwd issue, although permissions are a possibility.
Try using the full path to eos or setup the necessary environment in the script.
In unix shell "command not found" error comes when the directory at which command is located is not in PATH (an env variable) which is searched to locate the command.
The solution to this is either of any of the 2 approaches mentioned below
a) change PATH and export PATH variable either in shell startup files or user profiles
export PATH=$PATH:/eos-command-directory-location/
b) run the eos command in shell script you have developed with full path

Raspbian: Reset Bash environment variables

I was trying to get a crontab working on my Raspberry PI and I think I messed up my environment variables. I can execute a file from the GUI by right-clicking and choosing execute. However I cannot get the same file to run from command line. I can use ls to see the file (ChromeTab.sh), but when I type ChromeTab.sh, I get "bash: ChromeTab.sh: command not found".
I think I messed up my environment variables when I put this in the crontab.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
I followed the examples in Script doesn't run via crontab but works fine standalone.
Any idea what I'm doing wrong?
UPDATE:
OK,
Let me clarify what efforts I took on my part BEFORE posting my question on stackoverflow before getting anymore downvotes.
First of all thanks S. Adam Nissley for your suggestions.
In answer to your steps listed above.
Running this from home path, or fully qualified path does Not work as stated.
Error: bash: ChromeTab.sh: command not found
./ChromeTab.sh
I have also ensured read/write and execute permissions on the file with
chmod +x ./ChromeTab.sh
Also, my bash script starts off with the following shebang
#!/bin/sh
So, what i'm trying to say is, regardless of using crontab or not the issue at hand is that I can not even execute the script from command line. This started happening after I was messing around with the environment variables in the crontab. I'm looking for a way to revert to the situation where I can at least run/execute bash commands from the terminal.
The only way I can effectively execute this script is (right-click execute) through the GUI.
Assuming you are in the same directory as your script, you should just be able to enter
./ChromeTab.sh
If it does not execute, make sure it is executable with the command
chmod +x ./ChromeTab.sh
Or
chmod 755 ./ChromeTab.sh
And if it still won't execute, make sure it has an appropriate hashbang on the very first line of the script like #!/bin/sh or #!/bin/bash
When you add it to your crontab, make sure it has the full path like
/home/pi/bin/ChromeTab.sh <br/>
EDIT: Default PATH and SHELL for Raspbian
You can check your PATH and SHELL environmental variables from the command line as follows:
echo $SHELL
echo $PATH
The default PATH for Rasbian is:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
And the default SHELL is:
/bin/bash
So if you need to set those it is as simple as:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
SHELL=/bin/bash
If you are having other issues with your environment, you may want to disable some of your local settings to see if the problem is in your profile. You can list all files with ls -a, which includes hidden files. Raspbian typically has a .bashrc and a .profile in each user's home directory. To disable them simple rename them:
mv .bashrc .bashrc_disabled
mv .profile .profile_disabled
If that solves the problem, you can inspect the files and make the necessary corrections before renaming them back to their original names.

Activating a VirtualEnv using a shell script doesn't seem to work

I tried activating a VirtualEnv through a shell script like the one below but it doesn't seem to work,
#!/bin/sh
source ~/.virtualenvs/pinax-env/bin/activate
I get the following error
$ sh virtualenv_activate.sh
virtualenv_activate.sh: 2: source: not found
but if I enter the same command on terminal it seems to work
$ source ~/.virtualenvs/pinax-env/bin/activate
(pinax-env)gautam#Aspirebuntu:$
So I changed the shell script to
#!/bin/bash
source ~/.virtualenvs/pinax-env/bin/activate
as suggested and used
$ bash virtualenv_activate.sh
gautam#Aspirebuntu:$
to run the script .
That doesn't throw an error but neither does that activate the virtual env
So any suggestion on how to solve this problem ?
PS : I am using Ubuntu 11.04
TLDR
Must run the .sh script with source instead of the script solely
source your-script.sh
and not
your-script.sh
Details
sh is not the same as bash (although some systems simply link sh to bash, so running sh actually runs bash). You can think of sh as a watered down version of bash. One thing that bash has that sh does not is the "source" command. This is why you're getting that error... source runs fine in your bash shell. But when you start your script using sh, you run the script in an shell in a subprocess. Since that script is running in sh, "source" is not found.
The solution is to run the script in bash instead. Change the first line to...
#!/bin/bash
Then run with...
./virtualenv_activate.sh
...or...
/bin/bash virtualenv_activate.sh
Edit:
If you want the activation of the virtualenv to change the shell that you call the script from, you need to use the "source" or "dot operator". This ensures that the script is run in the current shell (and therefore changes the current environment)...
source virtualenv_activate.sh
...or...
. virtualenv_activate.sh
As a side note, this is why virtualenv always says you need to use "source" to run it's activate script.
source is an builtin shell command in bash, and is not available in sh. If i remember correctly then virtual env does a lot of path and environment variables manipulation. Even running it as bash virtualenv_blah.sh wont work since this will simply create the environment inside the sub-shell.
Try . virtualenv_activate.sh or source virtualenv_activate.sh this basically gets the script to run in your current environment and all the environment variables modified by virtualenv's activate will be available.
HTH.
Edit: Here is a link that might help - http://ss64.com/bash/period.html
On Mac OS X your proposals seems not working.
I have done it this way. I'am not very happy with solution, but share it anyway here and hope, that maybe somebody will suggest the better one:
In activate.sh I have
echo 'source /Users/andi/.virtualenvs/data_science/bin/activate'
I give execution permissions by: chmod +x activate.sh
And I execute this way:
`./activate.sh`
Notice that there are paranthesis in form of ASCII code 96 = ` ( Grave accent )
For me best way work as below.
Create start-my-py-software.sh and pest below code
#!/bin/bash
source "/home/snippetbucket.com/source/AIML-Server-CloudPlatform/bin/activate"
python --version
python /home/snippetbucket.com/source/AIML-Server-CloudPlatform/main.py
Give file permission to run like below.
chmod +x start-my-py-software.sh
Now run like below
.start-my-py-software.sh
and that's it, start my python based server or any other code.
ubuntu #18.0
In my case, Ubuntu 16.04, the methods above didn't worked well or it needs much works.
I just made a link of 'activate' script file and copy it to home folder(or $PATH accessible folder) and renamed it simple one like 'actai'.
Then in a terminal, just call 'source actai'. It worked!

Resources