Cygwin Shell Scripts - shell

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

Related

Having issues executing a makefile (run.sh) in git bash

I am trying to use git bash to run my .sh file that was generated using Makefile.
When running the command ./run.sh I get this message ./run.sh: line 1: /home/user/run: No such file or directory
To run a script file (using Git Bash), you do the following:
Add a "sh-bang" line on the first line (e.g. #!/bin/bash OR #!/usr/bin/env sh) this is how git bash knows a file is executable.
Use ./ (or any valid dir spec): ./script.sh
Note : any "sh-bang" will work
You are using git bash so I suppose you are using Windows.
As for me I always use shebang on my scripts. Depending on the content of your script, you may add one of the following lines at the first line of your script.
#!/bin/sh
#!/bin/bash
#!/usr/bin/perl
#!/usr/bin/tcl
#!/bin/sed
#!/usr/awk
#!/usr/bin/python
If you still have problems running the script with ./run.sh command, you may try to use sh run.sh (on Git bash) and it should execute the script just as ./run.sh does it on linux.
This error message says that the first line of the script tries to execute an executable program named run in your home directory, and this does not exist.
I don't know what run.sh is supposed to do, but if you want to execute it a program, you need to make sure that the program exists, for instance by creating it.

Problem in executing user defining shell commmand

I am trying to learn the basics of shell. I used vim editro for creating my own list of commands to be executed. Here is the way I created the code
vi mycommands
then inside this file I wrote
cd Documents
I am using macOS Catalina which has zsh by default but switched to bash
So when I write the following command in the terminal:
$ sh +x mycommands
It shows
+cd Documents
The Documents has some files and directories but it is not changing directory.Where am I going wrong?
Any help will be greatly appreciated.
Scripts run like sh myscript execute in a separate sub-shell, not the current shell. Changing directory inside a script will not cause your shell to change directory. If you want to change directory in your shell, you need to run the commands in your shell.
To do that, run:
. ./myscript (sh, bash) or source ./myscript (bash).
See this question.

Running gmt.script

I try to run a gmt-script and get the message:
bash-3.2$ plot_scenario.gmt
bash: plot_scenario.gmt: command not found
Does anyone know what could fix the problem?
I got a script from my supervisor, and it worked just fine on the uni Linux pc.
I have a Mac OS.
When you type text in shell, it tries to look for available commands. Here bash doesn't find any command like plot_scenario.gmt and outputs command not found.
The proper way to execute a file is ./filename where . refers to current directory. But you need to give execution permission to file you want to run. So, following commands may help you:
chmod +x <filename>
./<filename>
Note that make sure your pwd is where you're file is located. Another way to run or execute file is calling the shell:
bash /path/to/file

executable file doesn't print anything

I've written a simple script, that basically looks like this:
#!/bin/bash
echo Hello World
I'm trying to run this in my unix terminal but it basically does nothing. no errors, no printing, nothing
[solgag#t2 ~]$ olga
[solgag#t2 ~]$
any ideas?
Try ./test instead. If you run just test, bash will look for an executable named test in $PATH and it will find it (or maybe execute its own built-in?) as test is a standard command in UNIX.
if you shell script name is olga you need to run in terminal as
$./olga
To run the script as specified above you need to have executable permissions you can add executable permission using chmod command
$chmod u+x ./olga
You can also run a bash script using sh command
$sh olga

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