Unknown error when executing shell script - shell

I have the following shell script (bash script) at cron.sh file
#!/bin/bash
WORKON_HOME="/home/django/domains/example.com"
PROJECT_ROOT="/home/django/domains/example.com/django-project/"
. $WORKON_HOME/bin/activate
cd $PROJECT_ROOT
python manage.py cron
But when i run:
$ sh cron.sh
I got the following error
: not found
: not found
/bin/activatepen /home/django/domains/example.com
Server info
cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=9.10
DISTRIB_CODENAME=karmic
DISTRIB_DESCRIPTION="Ubuntu 9.10"
What am I doing wrong?

Your script has the wrong line endings. Pass it through dos2unix.

Well, you didn't show us everything in the code that you're trying to run. So I'll answer generically instead:
Run the script using sh -x cron.sh which will give you very verbose output of what it's doing up until the python invocation. If the errors are before that point, you know it's in the sh half and what caused them. If after that, you'll have to debug the python script.

Try using
bash -x cron.sh
or
./cron.sh
make sure to make it executable.

Related

Running into error with BASH script with permission denied, but when running the command directly in bash shell its getting executed

When running below command directly in bash shell, I am able to get the output. But when I am passing it via BASH script getting access denied. Any help would be appreciated
$ jq -r '.id' Repooutput.txt
dad04f6d-4e06-4420-b0bc-cb2dcfee2dcf
Error:
$ sh test.sh
test.sh: line 3: /c/ProgramData/chocolatey/bin/jq: Permission denied
I think the reason is that when executing the script with sh test.sh we're asking the POSIX interpreter (shell) to execute the content on the script, while when executing it with ./test.sh we're asking the script to "execute itself".
For the latter, the file needs to have execution permissions, which you can add with
chmod +x test.sh
Issue was with the naming convention of JQ inside BASH folder path, because of which the script was unable to pick the command. Renaming the JQ within BASH folder resolved this

How to run command after source env shell inside bash script

I am trying to run a command after setting up an environment. This command runs a python script which depends on the environment.
I have the following code:
#!/bin/bash
source ~/some/linux/env/shell
python test.py
However, the "python test.py" only runs after I exit the env shell.
I want to be able to run the "python test.py" inside this new shell env.
Firstly adding python running and interpreting directory.
#!/usr/bin/env python
And you have to give a code execution authority. You can give a permission below
chmod a+x test.py
Now you can run it from the command line.
./test.py

Why this command "google-auth" works in the terminal but not from bash script?

I have installed libpam-google-authenticator and freeradius on server ubuntu 16.0405. Everything works good, except for if I use the command google-auth in bash script I get a error message "google-auth: command not found"
But the same works if I put it on terminal directly.
#!/bin/bash
google-auth
That is not a bash script.
To make it a bash script, your first line needs to include a "#" as follows:
#!/bin/bash
google-auth
Also, you need to ensure that the script is executable:
chmod +x yourscript.sh
Hopefully that will solve your problem.
As per the comments below, it seems like the command "google-auth" was an alias which wasn't being established in the child shell.

setsid: can't execute './test.sh': Exec format error

I tried to execute setsid ./test.sh command on Alpine OS in a docker container.
And it says 'Exec format error'. then I added a header bang '
#!/usr/bin/env bash' to that script. It works.
But the same command and script without header on centos6 works well.
I've digged into it deeply. Seems that setsid is a system call which use execvp to execute command, execvp will treat the script as an execute file.
My script.sh has execute permission and can run with './script.sh'. Why does the script behave differently on alpine and centos?

Bash script: Turn on errors?

After designing a simple shell/bash based backup script on my Ubuntu engine and making it work, I've uploaded it to my Debian server, which outputs a number of errors while executing it.
What can I do to turn on "error handling" in my Ubuntu machine to make it easier to debug?
ssh into the server
run the script by hand with either -v or -x or both
try to duplicate the user, group, and environment of the error run in your terminal window If necessary, run the program with something like "su -c 'sh -v script' otheruser
You might also want to pipe the result of the bad command, particularly if run by cron(8), into /bin/logger, perhaps something like:
sh -v -x badscript 2>&1 | /bin/logger -t badscript
and then go look at /var/log/messages.
Bash lets you turn on debugging selectively, or completely with the set command. Here is a good reference on how to debug bash scripts.
The command set -x will turn on debugging anywhere in your script. Likewise, set +x will turn it off again. This is useful if you only want to see debug output from parts of your script.
Change your shebang line to include the trace option:
#!/bin/bash -x
You can also have Bash scan the file for errors without running it:
$ bash -n scriptname

Resources