Need ./ to execute scripts


 
Thread Tools Search this Thread
Operating Systems AIX Need ./ to execute scripts
# 1  
Old 05-21-2015
Need ./ to execute scripts

Hi,

I am really sorry for this question but still i am confused.

I have shell script called sample.sh

I can execute only with the combination of ./sample.sh

Is ./ really necessary ? Is it something related with $HOME or $PATH variable.

Why and How can i resolve this case ?

Regards,
Nantha
# 2  
Old 05-21-2015
Hello Nandy,

You are right on your thinking. The ./ before a script states that it is the one in the current directory to be run. If you just try to run it without any path at all then the shell (gives you the command line) will search each directory in turn down the definition of $PATH

You can adjust the value by adding this to your profile:-
Code:
PATH=$PATH:.

The colon : is the separator between entries and the dot . states the current directory.

This will mean that as you change directory, the scripts in the current directory become automatically available (subject to permission) and those you have left behind are not. If you want to always have your home directory looked at, you can add:-
Code:
PATH=$PATH:$HOME

or a combination of the two even:-
Code:
PATH=$PATH:.:$HOME

The order is important, so it needs a little thinking about on how you want to set it for your environment.


As to which profile you add it in to, well that depends on your shell.

Can you post the output from:-
Code:
echo $SHELL
ps -f



I hope that this helps,
Robin

Last edited by rbatte1; 05-21-2015 at 05:49 AM.. Reason: Prompt for confirming which shell, so which profile to adjust.
# 3  
Old 05-21-2015
Quote:
Originally Posted by Nandy
I can execute only with the combination of ./sample.sh

Is ./ really necessary ? Is it something related with $HOME or $PATH variable.

Why and How can i resolve this case ?
here is the short version: yes, it is necessary. Yes, it is related to the PATH variable. Yes, you could "resolve" that. No, you shouldn't do it anyway.

OK, here the same in long form:

In principle, every "command" you issue is an execution of a binary file: you probably have used the command "ls -l /some/place". This ultimately has called the binary "/usr/bin/ls" with the option "-l" and the argument "/some/place". Binaries can only be called giving the full path name of where they are. To stick with the above example "/usr/bin/ls". The reason is that there might be more than one binary named "ls" in different places. There would be no way to decide between "/usr/bin/ls" and "/some/other/place/ls".

I see you wondering now "why did ls -l work, then?" The question is legitimate. This is where the PATH variable enters the picture. "PATH" contains a list (separated by ":") of directories which are tried (in the same order as they are set in the variable). Whenever you enter a command the system cannot make sense of (like "ls" instead of "/usr/bin/ls") these pathes are tried to be prepended to the command and the system looks if it can find it there. Here is an example:

Code:
PATH="/usr/bin:/usr/sbin:$HOME/bin"
# command

The system - failing to understand "command" - will now do the following:

- look for "/usr/bin/command" and, if it exists, execute it, otherwise
- look for "/usr/sbin/command" and, if it exists, execute it, otherwise
- look for "$HOME/bin/command" and, if it exists, execute it, otherwise
- issue and error "command not found"

It is indeed possible to add the current directory to the path and make every executable in the current directory accessible by adding "." to the PATH:

Code:
PATH=/usr/bin:<and-so-on>:.

BUT: this is considered a very bad idea and so with a reason! If you are interested in details here is a thread obut it. In short making executables in any arbitrary directory ("." is where are right now, which can be everywhere) is a security problem and you are maybe not executing what you think you do (but something else instead).

Here is a better way: create a "bin" directory in your HOME-directory, then add this directory to your PATH. "cd" without any parameter will take you to your homedir, regardless of "$HOME" being set or not, "~" is short for "my home directory":

Code:
# cd
# mkdir ./bin

# PATH="${PATH}:~/bin" ; export PATH

If you want to have your PATH variable modified permanently put the last line into the file ~/.kshrc or whereever the variable "ENV" points to.

Whenever you have executable files put them into this directory and you can execute them without adding the path at all.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To execute scripts parallelly

Hi I have set two set of scripts sets in a file which perform similar operations but with different script names for e.g.: 1st set of script1.txt: 1.sh 2.sh 3.sh 4.sh 2nd set of script2.txt: 1_1.sh 2_1.sh 3_3.sh 4_4.sh I want to execute these set of scripts parallelly in such... (16 Replies)
Discussion started by: rohit_shinez
16 Replies

2. Shell Programming and Scripting

Execute scripts in Parallel

Hi I want to execute few scripts in Parallel. There is a Master Script (MS.ksh) which will call internally all the scripts we need to run in Parallel. Say there are three set of scripts : ABC_1.ksh --> ABC_2.ksh --> ABC_3.ksh (execute ABC_2 when ABC_1 is successful ; Execute ABC_3 only when... (6 Replies)
Discussion started by: dashing201
6 Replies

3. Shell Programming and Scripting

Script to execute multiple scripts

Hi All, I have 4 scripts to execute. Each one take anywhere from 3 -9 hours to run depending on what it's doing. I'm running one at a time then check back periodically to see if it's still going and if not, run the other. How can I put these scripts into another script so that they are... (5 Replies)
Discussion started by: bbbngowc
5 Replies

4. Shell Programming and Scripting

Need to execute different scripts with shell script

Hi All, Am in need of some inputs to overcome the following problem, A tar file(/var/execute/scripts/ that contains different types of scripts(may be perl,shell, python etc...). I have written a shell script which is located @ /var/execute.sh to untar the file in some location say... (1 Reply)
Discussion started by: SMNK
1 Replies

5. Shell Programming and Scripting

Execute scripts on remote server

Hi All, I need to first of all establish a connection to remote unix server non-interactively with the help of a shell script and then connect to oracle database from that server all with this script of mine. Please suggest the best method which could be used to connect to server for executing... (1 Reply)
Discussion started by: m_kapur83
1 Replies

6. Shell Programming and Scripting

need to have a cronjob which will execute certain scripts every hr

Hi My question needs two answers how to write scripts to update a table in oracle db based on the result of the number of record counts for example i need to execute the following script every hour awk '{sum++;}END{for(i in sum) {print i, sum}}' filename here everyhour the... (3 Replies)
Discussion started by: aemunathan
3 Replies

7. Shell Programming and Scripting

Execute scripts from remote using telnet

Hi having the below code (sleep 1 echo "username" sleep 2 echo "password" sleep 2 echo "cd /home/test1/path" sleep 10 echo "ls -l | grep filename" if exists echo "script1.sh filename" echo "mailx -s "Task Executed" user@domain.com" else echo "mailx -s "File not Exist"... (1 Reply)
Discussion started by: karthikn7974
1 Replies

8. Shell Programming and Scripting

Need to execute 2 scripts, wait, execute 2 more wait, till end of file

:cool: I need to execute a shell script to do the following: cat a file run two back ground processes using the first two values from the file wait till those background processes finish run two more background processes using the next two values from the file wait till those background... (1 Reply)
Discussion started by: halo98
1 Replies

9. Shell Programming and Scripting

How to execute scripts at logout?

Hi, We can execute scripts at X login time in Linux by placing scripts in /etc/X11/xinit/xinitrc.d/ directory. Can you suggest a method to execute scripts at X logout time? rgds, pcsaji (1 Reply)
Discussion started by: pcsaji
1 Replies

10. UNIX for Dummies Questions & Answers

How can I execute TCL scripts in HP-UX

Hi All, Since I want to execute TCL scripts in HP-UX, I cannot find where to start. I try to execute the following scripts. It gets "tclsh: not found." errors. Where / how to execute tcl scripts? Thanks a lots. #!/bin/sh # the next line restarts using wish \ exec tclsh "$0" "$@" (3 Replies)
Discussion started by: wilsonchan1000
3 Replies
Login or Register to Ask a Question