run typescript compiler from shell command - permission denied - macos

I'm using Parallels because I prefer the Mac OS but the work we do is all in Visual Studio. We currently have a build.cmd batch file that builds our typescript files. Because I'd prefer to work on the Mac side when I can, I thought I would rewrite the script in bash and to also get some experience writing a shell script. I have a main build.sh command that runs the other shell scripts like compile-templates.sh and compile-source.sh. I am trouble with the compile-source.sh portion now. Currently, the batch file looks like:
echo TypeScript Version:
CALL node_modules\.bin\tsc -v
The typescript compiler is included in our Solution so we are all using the same one throughout the solution. In my compile-source.sh, I try to do this:
node_modules/.bin/tsc -v
or this
./node_modules/.bin/tsc -v
And I get permission denied. Is there something I'm doing wrong?

There are several approaches to try. You can use bash to run the script like this:
bash node_modules/.bin/tsc -v
Or you can try to change the permissions on the file:
chmod a+x node_modules/.bin/tsc
This should enable you to run the script like this:
./node_modules/.bin/tsc -v
But in that case, make sure your script starts with a shebang line to tell the system it is a bash script:
#!/usr/bin/env bash

Related

How to run SDKMAN on shell script

I am trying to run sdkman on a shell script that I call run.sh. This is what the inside of the shell script looks like:
sdk use java 8.0.302-open
When I run the command in a terminal, it works. But when I run it in a shell script, I get this error:
run.sh: 1: sdk: not found
Anyone knows how to fix this?
I fixed it for me; Although this may not work for others.
I placed #!/bin/bash at the top of the shell script, and then added this after it:
source "$HOME/.sdkman/bin/sdkman-init.sh"
Then my shell script was able to be ran using:
./run.sh
sdkman was able to work this time.

Cygwin execution of .sh file can't find grep command?

So I was trying to create little .sh script for my work and run into one little problem.
My cygwin terminal (x64) runs just fine and I'm using it often enough to do manual greps.
In the begging I had some issues with this command but now it works fine in cygwin terminal.
Once I wrote my little script and tried to run it only output I'm getting is "line 6: grep: command not found"
My execution method is:
Open cygwin terminal
cd to script location
type in ./script.sh
enter :)
Anyone knows how to fix that? I already added cygwin bin folder to my system path (Win 10 btw) but that didn't helped. Looked around for a while but haven't found anything useful, mostly issues with grep itself.
my script for reference:
mkdir -p output
PATH=$PWD"/output"
while IFS=";" read -r component location global
do
cd $location
grep -iRl $global --exclude-dir={wrongdir1,wrongdir2} > $PATH"/"$component".txt"
done < input.csv
you're overwriting you Cygwin system path: PATH=$PWD"/output" - instead of PATH use a diff var name.

Script shell to automate deployment site

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)

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!

Cygwin Shell Scripts

I'm not running cygwin, but I have the cygwin ash.exe in my %PATH% as sh.exe and have cygwin1.dll in %PATH%
I am trying to invoke some shell scripts (named with no extension) using sh -c shell-script-name but I get a "permission denied" error. If I run sh and run ./script I also get this error. I have a proper #!/bin/sh shebang line and even renaming to .sh or .exe has no effect. What should I do?
One thing to try to see if Windows permissions are causing a problem is to run Process Monitor and filter it for sh.exe and shell-script-name. That will probably show you if there's particular permission you don't have (eg you might have read but not execute permission).
Try also running the shell interactively, ie:
c:\>sh
sh# . ./script or
sh# sh -c ./script
If this works then you know that the cygwin part is working correctly. Another thing to check is that the line endings for your script are unix, as that can stop scripts from executing correctly.
Everything worked for me after doing:
$ chmod +x script

Resources