Executing Shell Scripts?
If I create a simple Shell script, i.e.. [filename="logins"] with two simple commands. date and cal and I want to execute this basic shell script why cant I just type in [name file to be executed]
I'm running MDK 9.0. and using BASH shell.
I created a file/script called "logins", within this file I put:
So to run this script I first changed the permisision on the file to be a+x.
But when I type in logins all by itself I get:
[root@localhost SS]# logins
bash: logins: command not found
If I type "bash login then the file executes the commands within the login file. Simple shell script.
If I try to specifiy ./logins I get back:
[jbanks@localhost SS]$ ./logins
bash: ./logins: bin/bash: bad interpreter: No such file or directory
Why am I not able to just specify the "logins" and have it execute?
You have made a mistake in the first line of your script, replace #bin/bash with #/bin/bash (notice the first slash). This should solve the problem when executing ./logins
If you want to type logins in the command line without ./ , you have to copy your script to /usr/bin or whatever path that is in your PATH.
btw, to view all pathes in your PATH variable, just type "echo $PATH"
Hope this helps
What exactly is a+x?
where can I read some info on a+x?
Thank you in advance for any comments.
Basically any file/directory in linux has 3 permissions, which are the permission to read, the permission to write and the permission to execute/run. Try to run the command "ls -l" and you should be able to see something like this on the left.
These are the permissions of a file. r stands for read, w for write and x for execute. The first 3 letters tell about the permissions of the owner of the file on it. The second 3 letters specify the permissions of the group that the file belongs to. The last 3 letters set the permissions of other people on that file.
Now, you can change the permissions of the file using chmod. And "chmod a+x" give the execute permission to all users. (a for all) Read more on this in any linux book.
Attention to detail is the name of the game in the Unix/linux world.
So now I'm wondering why it is that I have to have my "logins" script located in the /usr/bin directory for it to execute when only specifying the script by script file name?
I have two books that I'm going through right now about shell scripting, and I don't beleave that the mention this little tidbit. Any "DOC'S" that you can point me to that do mention this will help?
I'm just learning. So any helpful hints are welcome.
Thanks Hfawzy, :D
When you execute a program without ./ , your system tries to find the requested program located in your PATH. The PATH enviroment variable contains some pathes, these pathes are delimited with ":". So, if you move your script from /usr/bin to any other path in your PATH, this will also work.
If you want to find any documentation/tutorial related with any subject in linux (enviroment variables...), just search www.google.com/linux and you will find and learn a lot ;)
Thank you very much. will do.
Thanks for the reply Hfawzy.
My $PATH= when logged as as normal user:
[jbanks@localhost jbanks]$ echo $PATH
When logged in as root:
[root@localhost jbanks]# echo $PATH
.....so because the location of the "logins" script is located at /home/jbanks/SS this isn't in either the local users path or roots path? Correct or not?
I'm pretty sure this is correct....:)
So just out of example, (and I'm sure they're are other ways to do this) from my local user, (non-root), to get the "logins" script to execute just by typing "logins" I would need to add /home/jbanks/SS to my $PATH, or just /home?
Just unsure if I put /home, will it decend through the /home directory and all of its subdirectories?
Thanks for helping me get a grip on this. Much appreaciated :P
|All times are GMT -5. The time now is 08:54 PM.|