What is wrong with this .command file? - bash

This is my script to automate the git push of my static blog.
When I run each command one by one in the Terminal, it works. Do I need to add delays, maybe it's going too fast. The pelican command (static website generator) takes quite a lot of time (2 seconds). Is the rest of the script crashing during that?
#!/bin/sh
dropbox
cd blog
pelican . -s /Users/Paul-Arthur/Desktop/Desktop/Dropbox/blog/pelican.conf.py -t subtle
cd output
git add .
git commit -m 'commit'
git push
Updated: Sorry, yeah dropbox is a custom command in my bash_profile (this is not the problem, it works I know ;) ).
Sadly, when I click my script, it executes (but does not work) extremely quickly so I cannot see the errors.
Here is the output from the calepin command. The errors are normal and I expect it to run with that. Do you think that this is the problem? If so what can I do?
familys-imac:blog Paul-Arthur$ pelican . -s /Users/Paul-Arthur/Desktop/Desktop/Dropbox/blog/pelican.conf.py -t subtle
ERROR: Skipping ./articles/aboutme.md: impossible to find informations about 'title'
ERROR: Skipping ./articles/static_sites.md: impossible to find informations about 'title'
familys-imac:blog Paul

It might be due to the « cd » command, since it's not a command, it is a builtin from the shell, and doesn't act like a command.
To debug it, try adding «pwd» command inside your script before and after the «cd» line, to be sure the working directory has change.
It could also be due to the shell you are using, in the shebang (first line of your script), you are using the /bin/sh script. Is it the good one ? When you do it in your shell, you are maybe using another like bash, dash, zsh etc.
To determine that, type that in your current shell :
which `echo $0`
You will get an answer like :
/bin/bash
or something like this. Use this in your shell script :
#!/bin/bash
And try again your script.
Good luck with your project.

Related

Can't start script via screen command

I have a script, and I want to make it work in background after boot. Therefore, I added the command below into rc.local. However, it doesn't work.
sudo -iu executerroot screen -dmS test bash -c "bash /home/pi/FileServer/Run; exec bash"
Also I've tried commands in https://askubuntu.com/questions/261899/run-a-screen-session-on-boot-from-rc-local site but no one worked for me. If I execute
sudo -iu executerroot screen -dmS test bash -c "bash /home/pi/FileServer/Run; exec bash"
I get this:
bash: /home/pi/FileServer/Run/: No such file or directory
while it doesn't work for real files. When I type "screen -r" I get just blank bash screen if a file exists.
I can't figure out why it doesn't work. Please explain why it happens and how can I solve this.
Thanks...
After years, I got a similar problem today. This time I was trying to call the screen program inside crontab. I realized I needed absolute paths and correct bash version in crontab as well as in the script itself.
I think I had done a similar mistake 6 years ago. I wrote similar program today, probably, due to different environment variables, script didn't be able to find programs (in my case openvpn). Even program &> output.txt didn't work. The problem was the different bash version not supporting output redirection. I used /usr/sbin/bash -c "command" to cope with it. Also, openvpn is not in the path in that crontab environment, so I had to call it with absolute path (like /usr/sbin/openvpn). Then, it worked.

Very basic git batch in Windows 7

Synopsis
Pass arguments to git bash using a .bat file
OR
Use git some other way by running a batch file.
Disclaimer
There are a lot of answers to similar questions out there, but having tried and failed most or all of them, I hope you don't smack me with a DUPLICATE stamp at first sight.
My system
Windows 7, 64 bit
git version 1.9.4.msysgit.2
The challenge
I want to rationalize my workflow from navigating to a number of git repos and running git status, git add --all etc, to simply run a batch.
Other posts certainly suggests it's possible. I think most of them use Unix, but this one at least got me up and running on Windows so I could test it out. However, I'm having the same problems as OP in that post when it comes to passing the commands to the git bash, and to a complete beginner like me it seems a bit complicated to use the suggestions from #Erik further down in the same post if you want to include more commands in the work flow.
Following the suggestions from #inf3rno in the same post, it seems I'm able to change folders but not able to use git commands like this:
set bash=C:\Program Files (x86)\Git\bin\bash.exe
"%bash%" --login -i -c "exec "%1""
cd c:\repos\research
git status
pause
Does anyone have suggestions as to how I can change the code above to get it working?
And by the way, what's the deal with #!bin\bash from other posts like this one? I assumed I had to use #!Program Files (x86)\Git\bin\bash, but I had no luck with that either...
Anyway, thank you for any suggestions!
First, uninstall git 1.9.4 (msygit, which is now obsolete): git-for-windows will offer a much recent bash (2013 vs. 2005).
Simply unzip PortableGit-2.6.1-64-bit.7z.exe anywhere you want, add C:\path\PortableGit-2.6.1-64-bit\bin to your %PATH% and you are good to go.
Second, inf3rno's answer is about executing any bash script ("%bash%" --login -i -c "exec "%1"": the %1 is the path/name of the bash script to be executed)
The right shebang to use in your bash scripts would be #!/bin/bash (see "What is the preferred Bash shebang?")
With the latest git 2.6, that would be:
c:\prgs\git\PortableGit-2.6.1-64-bit\bin\bash.exe --login -i -c "exec ./myscript"
Since that folder is supposed to be in your %PATH%:
bash --login -i -c "exec ./myscript"
With myscript being a file using Unix-style eol (LF), not Windows-style (CRLF)
Note also that any bash script (even on Windows) called git-myscript can be directly called with:
git myscript
I described in 2012 another approach in "Running a batch file in git shell" for executing git command.
But for a pure bash script, you will want to go with bash --login -i -c "exec ./myscript".
For writing bash scripts with unix eol style, you can choose various editor from Notepad++, SublimeText 3 or Atom.io.

How to make a "macro" to automate some bash commands in OS X Terminal?

I'm pretty decent with bash and UNIX commands, and the Terminal app, but I'm wondering, is there a way to make a "macro" (maybe that's the wrong word!) to automate some tasks?
For example, to get into my current project directory, I type:
$ cd ~/Documents/College/F13/CS362/lab3/os-lab-3
And then I immediately do
$ hg pull
(password)
And then
$ hg update
So is there a way to even automate that first step of the cd call? I've never tried anything like that before, not sure if what I'm going for is even possible.
Bonus: is there a way to have the macro enter my password when prompted as well? Security/privacy is not really an issue here, there's no thermonuclear codes hiding around in the repo.
The name generally used in this context (a macro in bash) is shell script. To automate the commands from your post you would need to create a file with a name like 'myscript.sh' with the following contents:
#!/bin/bash
cd ~/Documents/College/F13/CS362/lab3/os-lab-3
hg pull
hg update
The first line of the script is a hashbang. This is a special comment line which indicates that file should be executed using the indicated program. Bash in this case.
To run the script first ensure that it is executable by running the following command:
chmod +x myscript.sh
From there in terminal window just run the script by specifying the path to it:
./myscript.sh
Don't put passwords into shell scripts though! It is a bad habit. For authentication with Mercurial an ssh key pair is the best way to go. The ssh key can be loaded into OSX's keychain, so you won't have to type anything when you run the script. You'll also need to add the public portion of your ssh key pair to the list of authorized hosts on the remote Mercurial repository.

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!

Getting a 'source: not found' error when using source in a bash script

I'm trying to write (what I thought would be) a simple bash script that will:
run virtualenv to create a new environment at $1
activate the virtual environment
do some more stuff (install django, add django-admin.py to the virtualenv's path, etc.)
Step 1 works quite well, but I can't seem to activate the virtualenv. For those not familiar with virtualenv, it creates an activate file that activates the virtual environment. From the CLI, you run it using source
source $env_name/bin/activate
Where $env_name, obviously, is the name of the dir that the virtual env is installed in.
In my script, after creating the virtual environment, I store the path to the activate script like this:
activate="`pwd`/$ENV_NAME/bin/activate"
But when I call source "$activate", I get this:
/home/clawlor/bin/scripts/djangoenv: 20: source: not found
I know that $activate contains the correct path to the activate script, in fact I even test that a file is there before I call source. But source itself can't seem to find it. I've also tried running all of the steps manually in the CLI, where everything works fine.
In my research I found this script, which is similar to what I want but is also doing a lot of other things that I don't need, like storing all of the virtual environments in a ~/.virtualenv directory (or whatever is in $WORKON_HOME). But it seems to me that he is creating the path to activate, and calling source "$activate" in basically the same way I am.
Here is the script in its entirety:
#!/bin/sh
PYTHON_PATH=~/bin/python-2.6.1/bin/python
if [ $# = 1 ]
then
ENV_NAME="$1"
virtualenv -p $PYTHON_PATH --no-site-packages $ENV_NAME
activate="`pwd`/$ENV_NAME/bin/activate"
if [ ! -f "$activate" ]
then
echo "ERROR: activate not found at $activate"
return 1
fi
source "$activate"
else
echo 'Usage: djangoenv ENV_NAME'
fi
DISCLAIMER: My bash script-fu is pretty weak. I'm fairly comfortable at the CLI, but there may well be some extremely stupid reason this isn't working.
If you're writing a bash script, call it by name:
#!/bin/bash
/bin/sh is not guaranteed to be bash. This caused a ton of broken scripts in Ubuntu some years ago (IIRC).
The source builtin works just fine in bash; but you might as well just use dot like Norman suggested.
In the POSIX standard, which /bin/sh is supposed to respect, the command is . (a single dot), not source. The source command is a csh-ism that has been pulled into bash.
Try
. $env_name/bin/activate
Or if you must have non-POSIX bash-isms in your code, use #!/bin/bash.
In Ubuntu if you execute the script with sh scriptname.sh you get this problem.
Try executing the script with ./scriptname.sh instead.
best to add the full path of the file you intend to source.
eg
source ./.env instead of source .env
or source /var/www/html/site1/.env

Resources