Unix Newbie question....

gopunk

Lifer
Jul 7, 2001
29,239
2
0
so here at work the consultants write these perl scripts that we use, and you just go to the prompt and type something like "owner text" where owner is the command and text is some owner that you want to look up, and it works no matter what directory you're in.

how does this work? i tried writing a hello world perl script, but when i typed in the filename, nothing happened. all the file has in it is:

#!/usr/local/bin/perl
print "hello world\n";

do i have to do something special to run from cmd line?
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
Did you give it proper permissions? And is that the proper format for perl? And did it give any errors?

And this may be better in Software forum, or at the worst OS. :p
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
yea chmod 755, and it does nothing. test is the name of my program:

red1_28:thedarby% test
red1_29:thedarby%

no errors, no nothing. is it possible that it has to be in a special directory before it can be executed? i'm heading home now, i'll check back when i get home...
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
Any directory should be fine as long as you have the path to the perl interpriter correct.
 

jobberd

Banned
Mar 30, 2001
2,057
0
0
I couldn't see anything wrong with your code, so I tried it out myself and it worked. make sure that it is in a directory that is mentioned in the PATH variable, or if not, write the full directory (ie. /home/perl.script)

edit: nm, if it were in a directory not in the PATH variable it would give a message similar to "*** command not found"
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
if you want to execute it from anywhere, you'll have to but it in a directory that the OS looks for executables in. i.e, /usr/bin/ or /usr/local/bin/

if you jst want to test the perl script, but don't have access to those directories, do this: perl /path/to/my/file.pl
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0


<< Try ./test.pl and see if that works. >>



okay that worked! but why? what does the ./ do? how do i make it so i don't need it? and how come my file has a little star next to it when i ls? "test.pl*"
 

jobberd

Banned
Mar 30, 2001
2,057
0
0


<< okay that worked! but why? what does the ./ do? and how come my file has a little star next to it when i ls? "test.pl*" >>

the . is a symbolic link to the current directory you are in, so if you were in /home/whateveryournameis/scripts, instead of typing /home/whateveryournameis/scripts/perl.script, you could just type ./script.
the little star next to your script indicates that you switched on the executable flag and it can be run. otherwise you couldn't run it just by typing in ./perl.script
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0


<<

<< Try ./test.pl and see if that works. >>



okay that worked! but why? what does the ./ do? how do i make it so i don't need it? and how come my file has a little star next to it when i ls? "test.pl*"
>>



Im going to answer these out of order ;)

The * should mean its executable or its a text file. Look at "man ls" for the -F flag information.

./ means in this directory. . is the current directory, .. is the directory above that one. Adding . to the end of your path will fix it, but its dangerous, especially as root. Just get used to ./ and you should be ok.

EDIT: jobberd got it before I did (had to check on the * to figure out what the heck I was saying at all). But definitely check out the man page for ls (there are a *LOT* of options 25-26 something like that). -F being the best (in my opinion of course).
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
thanks jobberd and n0cmonkey!


./ means in this directory. . is the current directory, .. is the directory above that one. Adding . to the end of your path will fix it, but its dangerous, especially as root. Just get used to ./ and you should be ok.

hmm... two more questions.

if i just type in the name, without the ./, then what is it doing if it's not looking in the current directory?
also, why is dangerous?
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0


<< thanks jobberd and n0cmonkey!


./ means in this directory. . is the current directory, .. is the directory above that one. Adding . to the end of your path will fix it, but its dangerous, especially as root. Just get used to ./ and you should be ok.

hmm... two more questions.

if i just type in the name, without the ./, then what is it doing if it's not looking in the current directory?
>>



It goes through the $PATH (echo $PATH) variable looking for whatever you typed. If I was on my UNIX-like machines now Id give you an example.... Since . (current directory) is not in your path it will not look there for the file you specify).



<< also, why is dangerous? >>



Sometimes things get named strange names. If you mean to run some file you stupidly named rm in your directory, which was a script that checked disk usage you could end up deleting the wrong thing. Ive made stupid mistakes (not quite that bad, but I couldnt think of an extreme really :p) and they are not fun. Plus, putting a path (full or relative) is a good idea incase someone slipped a trojan of some sort in another part of the disk. Ive trojaned accounts like that before. Its not elite, but it gets the job done ;)
 

jobberd

Banned
Mar 30, 2001
2,057
0
0


<< if i just type in the name, without the ./, then what is it doing if it's not looking in the current directory?
also, why is dangerous?
>>

It looks through all the different directories that are listed in the PATH directories (in order). Type 'echo $PATH' (in Bash I believe) to see all the different directories. If it isn't in any of those, then the shell won't bother lookinganywhere else. It's dangerous because a malicious user could (for example) set up a harmful script, place it in your home directory, and rename it to a commonly used program. Then whenever you ran that program the script would be activated first and cause whatever damage. This example is easy to circumvent, but there are other methods that aren't so easy to stop
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
It goes through the $PATH (echo $PATH) variable looking for whatever you typed. If I was on my UNIX-like machines now Id give you an example.... Since . (current directory) is not in your path it will not look there for the file you specify).

aha, so if i place my little script in whatever $PATH is, i won't need the ./?

Sometimes things get named strange names. If you mean to run some file you stupidly named rm in your directory, which was a script that checked disk usage you could end up deleting the wrong thing. Ive made stupid mistakes (not quite that bad, but I couldnt think of an extreme really :p) and they are not fun. Plus, putting a path (full or relative) is a good idea incase someone slipped a trojan of some sort in another part of the disk. Ive trojaned accounts like that before. Its not elite, but it gets the job done ;)

ah i see :)

more dumb questions (sorry, but all they taught in middle school was hypercard)...

when you say add . to the end of the path... is the path the first line of the program? or is it something else? also, does having the safe path make it so that anybody who uses the program doesn't have access to higher up directories? is that why it's harder to slip trojans in?
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
It looks through all the different directories that are listed in the PATH directories (in order). Type 'echo $PATH' (in Bash I believe) to see all the different directories. If it isn't in any of those, then the shell won't bother lookinganywhere else.

thanks, trying that now :)
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0


<< It goes through the $PATH (echo $PATH) variable looking for whatever you typed. If I was on my UNIX-like machines now Id give you an example.... Since . (current directory) is not in your path it will not look there for the file you specify).

aha, so if i place my little script in whatever $PATH is, i won't need the ./?
>>



yep

put it in /usr/bin or any other dir thats in your path, and then you can just type the filename because it will be found in your path.
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
export $PATH:. should fix it temporarily, look through your bashrc file or something to edit it for all time.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0


<< export $PATH:. should fix it temporarily, look through your bashrc file or something to edit it for all time. >>


~/.bashrc i believe is the filename, just look in there and find where it sets your path, and then just append to it following the same syntax thats there.
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
hmm... i can't find the bashrc file. btw what does ~ do?

but i did the echo $PATH and this is what came up:

red0_22:bin% echo $PATH
/usr/redusers/thedarby/bin:/usr/local/bin:/usr/ndc/bin:/usr/ucb:/bin:/usr/bin:/u
sr/sbin:/sbin:/usr/bin/X11:.

i get the feeling i'm only supposed to be operating in the first entry (/usr/redusers/thedarby/bin). there was no bin directory, so i made one, and chmoded it 755. then i put the test.pl in there. but it's a no go. did i do something wrong?
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0


<< export $PATH:. should fix it temporarily, look through your bashrc file or something to edit it for all time. >>



cool, once i find the bashrc file, i'll do that. thanks.
 

jobberd

Banned
Mar 30, 2001
2,057
0
0
the .bashrc file has a . in the front of it, so it's hidden from normal searches. type 'ls -a' to list all files. Oh, and .bashrc only loads up on non-login shells. Change your path in ".bash-profile". I don't know whats wrong otherwise
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
heh i know why. i have a .cshrc. and when i try to chsh, it says it's disabled. *shrug* i didn't know csh had tab filename completion...
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
Ok, so you are using csh. Much better than BASH in my opinion, but then again most shells are ;)

You can change your path in .cshrc.



<< red0_22:bin% echo $PATH
/usr/redusers/thedarby/bin:/usr/local/bin:/usr/ndc/bin:/usr/ucb:/bin:/usr/bin:/u
sr/sbin:/sbin:/usr/bin/X11:.
>>



It looks like it already has the current directory in the $PATH variable, so Im not sure why it isnt working. Your shell will not go farther into these directories looking for files, but just in the directories specified.

./ is easy enough.



<< hmm... i can't find the bashrc file. btw what does ~ do? >>



~/ is a shortcut for your home directory. It may not work on all UNIXes and UNIX-like OSes, so dont rely on it.



<< the .bashrc file has a . in the front of it, so it's hidden from normal searches. type 'ls -a' to list all files. Oh, and .bashrc only loads up on non-login shells. Change your path in ".bash-profile". I don't know whats wrong otherwise >>



Thanks for the correction, I dont even bother installing BASH. KSH is the best :p