[SOLVED] how to run shell script in any folder except the folder which contains the shell script
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 09:13 PM.
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.
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
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
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-19-2015 at 11:17 PM.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.