LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-19-2015, 09:36 PM   #1
Mike_Brown
Member
 
Registered: May 2015
Posts: 37

Rep: Reputation: Disabled
how to run shell script in any folder except the folder which contains the shell script


I am using a shell script named by test.sh, for example containing
Code:
address="$PWD"
echo "$address"
.
If I put it in folder temp1, and run test.sh, then it will give me the address of the current directory. But if I am now in folder temp2, and I want to run test.sh, I always need to copy test.sh to folder temp2, and then run it. Is there a way that I can run test.sh without copying it? I am not root user.

Last edited by Mike_Brown; 05-19-2015 at 10:13 PM.
 
Old 05-19-2015, 10:20 PM   #2
wpeckham
Senior Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 1,660

Rep: Reputation: 584Reputation: 584Reputation: 584Reputation: 584Reputation: 584Reputation: 584
NAturally

There is no basic difference between your script and any system script, except that it IS yours.

Check your environment variable PATH. IF your home is /home/mine and there is a string ;/home/mine/bin in your PATH, then put your script there and you can then execute it from anywhere. If that folder does not exist, create it with mkdir. (Make sure it is not writeable by group or world, this is JUST for you!)

To check your path, try "echo $PATH".
If said folder is NOT in your path, we can discuss how to modify a profile or rc file for you to add it properly. (The location to do that is somewhat shell specific, and you did not say which shell you run.)

The other locations in your path are locations where the system adds programs or scripts for some or all users to run. If you do not want EVERYONE on a machine to be able to run your script, NEVER put it in one of those folders. There is an obvious converse: if you want EVERYONE to have access, drop it in one of those places. (Most commonly, /usr/local/bin. /bin and /usr/bin are somewhat reserved for system and installed repo software that are maintained, rather than locally produced.)

I hope that this helps. Let me know if I was not clear.
 
2 members found this post helpful.
Old 05-20-2015, 12:09 AM   #3
Mike_Brown
Member
 
Registered: May 2015
Posts: 37

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by wpeckham View Post
There is no basic difference between your script and any system script, except that it IS yours.

Check your environment variable PATH. IF your home is /home/mine and there is a string ;/home/mine/bin in your PATH, then put your script there and you can then execute it from anywhere. If that folder does not exist, create it with mkdir. (Make sure it is not writeable by group or world, this is JUST for you!)

To check your path, try "echo $PATH".
If said folder is NOT in your path, we can discuss how to modify a profile or rc file for you to add it properly. (The location to do that is somewhat shell specific, and you did not say which shell you run.)

The other locations in your path are locations where the system adds programs or scripts for some or all users to run. If you do not want EVERYONE on a machine to be able to run your script, NEVER put it in one of those folders. There is an obvious converse: if you want EVERYONE to have access, drop it in one of those places. (Most commonly, /usr/local/bin. /bin and /usr/bin are somewhat reserved for system and installed repo software that are maintained, rather than locally produced.)

I hope that this helps. Let me know if I was not clear.
Thanks for helping me. The output the folder I want to run shell script is
Code:
[zwang10@c15-0330-01 K_10]$ echo $PATH
/opt/rust/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/opt/FEniCS/v1.4.0/bin:/usr/lib64/openmpi/bin:/usr/local/cuda-6.0/bin:/usr/lib64/ccache:/usr/local/bin:/bin:/usr/bin:/usr/sbin:/sbin:/usr/lib64/qt-3.3/bin:/home/campus27/zwang10/bin:/opt/grails/v2.4.3/bin:/opt/groovy/2.3.4/bin:/opt/seisan/v10.1/PRO:/opt/SeismicUnix/43R5/bin
The computer c15-0330-01.ad.mtu.edu belongs to campus. I am trying to copy qgenscript.sh to /usr/kerberos/sbin (although I do not who is kerberos). I got
Code:
[zwang10@c15-0330-01 K_10]$ cp qgenscript.sh /usr/kerberos/sbin
cp: cannot create regular file `/usr/kerberos/sbin/qgenscript.sh': Permission denied
I also did
Code:
cp qgenscript.sh /home/campus27/zwang10/bin
Where zwang10 is my user name.
There is no error when copy qgenscript.sh to /home/campus27/zwang10/bin.
But when I want to apply shell script to another folder, I got
Code:
./qgenscript: Command not found.
About which shell I am using
Code:
[zwang10@c15-0330-01 K_40]$ echo $SHELL
/bin/tcsh

Last edited by Mike_Brown; 05-20-2015 at 12:17 AM.
 
1 members found this post helpful.
Old 05-20-2015, 06:19 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
Code:
[zwang10@c15-0330-01 K_10]$ cp qgenscript.sh /usr/kerberos/sbin
cp: cannot create regular file `/usr/kerberos/sbin/qgenscript.sh': Permission denied
Probably means you don't have write perms on the dir

Its not entirely clear what you mean by
Quote:
apply shell script to another folder
or where you are trying to run it from, but fyi it is possible to have a disk partition mounted as 'noexec'; this means it is not possible to execute anything residing on that partition.

HTH
 
1 members found this post helpful.
Old 05-20-2015, 07:41 AM   #5
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by Mike_Brown View Post
But when I want to apply shell script to another folder, I got
Code:
./qgenscript: Command not found.
The ./ at the begining of the command means the script must be in your current working directory. If you are not in /home/campus27/zwang10/bin when executing this, it's not going to work. So (since /home/campus27/zwang10/bin is in your $PATH) just leave out the ./ and you'll be fine.
 
1 members found this post helpful.
Old 05-20-2015, 07:48 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,958

Rep: Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530
kerberos is a network authentication protocol. Not sure what you are trying to accomplish but I doubt you have write permissions to directories other then your home which I assume is /home/campus27/zwang10/. As stated you can run your program other then the current working directory if it resides in your path environment.

If you copy your script to /home/campus27/zwang10/bin then you can run it from anywhere by just typing in the command
qgenscript

If the script is located in a directory not in your path environment then you can run it from that directory by using the command
./qgenscript

The . is a shortcut for current working directory.

http://en.wikipedia.org/wiki/Kerberos_28protocol%29
 
1 members found this post helpful.
Old 05-20-2015, 08:26 AM   #7
wpeckham
Senior Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 1,660

Rep: Reputation: 584Reputation: 584Reputation: 584Reputation: 584Reputation: 584Reputation: 584
Almost there....

try typing
Code:
qgenscript.sh
in any folder.
You have the script in yoru home bin, and that IS in your execution path. You should be able to call it now, but (as mentioned above) not with the ./ prefix.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to run another command from within shell script, when run from home directory blackray1 Linux - Newbie 7 07-01-2013 10:42 AM
Shell script for run an shell script on server using ssh bloodstreetboy Linux - Server 5 01-12-2013 04:23 AM
in bash shell how to run shell script during startup rammohan04 Red Hat 2 07-31-2009 03:07 AM
MySQL Updates With Null When Perl Script Run From Shell Script ThisGuyIKnow Programming 6 08-12-2008 10:56 AM
Shell Script to Get Folder Names firefoxlinux Programming 6 09-27-2007 03:56 PM


All times are GMT -5. The time now is 03:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration