Commands not working in a script - linux

I'm trying to write my first bash script to automate some boring stuff I have to type everytime but can't get it working.
I've created pgAdmin.sh in my home directory:
#!/bin/bash
cd /opt/enviromentpy/pgadmin4
source bin/activate
python lib/python2.7/site-packages/pgadmin4/pgAdmin4.py
When I run it using ./pgAdmin.sh I get:
./pgAdmin.sh: line 2: cd: /opt/enviromentpy/pgadmin4: No such file or
directory
./pgAdmin.sh: line 3: bin/activate: No such file or directory
python: can't open file 'lib/python2.7/site-packages/pgadmin4/pgAdmin4.py': [Errno 2] No such file or directory
But when I open terminal and just put those commands one by one from Home directory it works just fine.

You have made a simple spelling mistake.
Instead of enviromentpy you probably meant to write enviromentpy (notice the extra n)

Related

bash: No such file or directory when file is clearly present

I have a shell script located at "/home/pi/scripts/take-snapshot.sh" but when ever I try to execute it I get a error that the file is not present.
the following commands do not work (assuming in script directory):
/home/pi/scripts/take-snapshot.sh
./take-snapshot.sh
take-snapshot.sh
bash /home/pi/scripts/take-snapshot.sh
the following do work and will bring up the shell file (not a new file):
vi take-snapshot.sh
nano take-snapshot.sh
The most likely cause is that your file is not executable. Bash is a bit confusing in that it reports the file as "not found", even though you only don't have permissions to execute it. Run ls -l and check the permissions. The leftmost column should show an "x" at least for the current user. It will usually look something like -rwxr-xr-x for a file you have created yourself.
Run chmod +x take-snapshot.sh to fix the permissions if they don't match.
I have seen this error when the line endings are windows EOL characters. It doesn't give any other error, just the above "No file or directory".
Check the EOL character and convert it to linux EOL, if it is windows and try to run the script again.

In my shell script I would like to list all the files in my current directory

In my shell script I would like to list all the files and directories in my current directory.
I know the command is ls, but I have no idea how to run it in a shellscript.
Thanks for anyhelp.
So you're basically asking: what is a shell-script, how do I create one, and how do I run it ....
Use your editor of choice to create a file, give it the following content:
#!/bin/sh # change to your preferred shell, sh being a low common denominator
command # in your immediate question that would be `ls`
Save the file.
Run chmod u+x file (not the word file, but what you called your saved script).
Then you can execute your file like so:
./file

Opening Exec from different directory

Beginner linux user here,
I want to create an sh file which will open firefox, file explorer and sublime text, rather than execute these commands separately. I have created a bin folder in /home/user and have saved my .sh file there.
Everything runs as I want to except for running the sublime_text executable.
It cannot find the directory as I am running the sh file from the bin directory.
So, my question is, how can I open sublime text from another directory without creating another shell process to do so.
#!/bin/bash
cd /home/user/
./Documemnts/sublime_text_3/sublime_text
xdg-open ~/Documents/sublime_text_3/sublime_text
firefox -new-tab -url https://www.google.com
xdg-open Documents/Work/
I get No such file or directory
or
error: error opening location: No application is registered as handling this file
It looks like the directory on line 2 is misspelled:
./Documemnts/sublime_text_3/sublime_text
Notice Documemnts vs. Documents

Cygwin bash files

About a year ago, I created a couple text files called "compile" and "pull." When I go into a cygwin prompt and type those names and hit enter (basically use them as a command), the cygwin terminal runs what is in those text files. For instance here is the contents of one:
git checkout master
git checkout -- .
I don't even remember how I did this. I'm pretty sure this is not a bash script.
I do remember that I had to not just create the file in notepad but also perform some linux command line operation on it, in order to use it. Once I did that I could basically use the file as a command.
In *nix, you have to make a file executable in order to be able to run it:
chmod u+x file
You also need to add the path to the file to the PATH variable
PATH=$PATH:/path/to/the/file
or, add . to always scan the current directory for commands (it's considered unsecure, though):
PATH=$PATH:.

can't source script in a current directory

So apparently, I can't source a script if that script is in the current directory. For example,
# source some/dir/script.sh
Ok
works fine, but if I'm in the same dir as the script, it errors out:
# cd some/dir
# source script.sh
-sh: source: script.sh: file not found
What gives? Is the only way around this to change directory?
I'm using bash v4.2.10 on Angstrom Linux if that's relevant.
Quoting the source man page:
source filename [arguments]
....
If filename does not contain a slash, file
names in PATH are used to find the directory containing file-
name.
So... source is trying to search your script.sh in the folders contained in PATH.
If you want to source a file in the current folder use
source ./script.sh
Use an absolute path -- source /root/path/to/some/dir/script.sh -- should sort you.
This can happen when the file is in the wrong format. I FTP'd a Korn Shell script from Windows. I could edit it, but got "not found [No such file or directory]" when I tried to run it. It turned out it was in DOS format, which was indicated in the file name line when I edited it in vi. After I re-FTP'd it, making sure it was being transferred as ASCII, it ran fine.

Resources