View Full Version: Linux shell scripts

C++ Learning Community > Computer Lab > Linux shell scripts


Title: Linux shell scripts
Description: and gcc


dr voodoo - May 30, 2004 08:49 PM (GMT)
I have been messing around with linux, but have come accross a problem how do I make a shell script? I though this would work but it doesn't:
CODE
#echo echo hello > temp
#temp

I though that would now print hello but it doesn't. What am I doing wrong?

Also I've been messing around with gcc on the commandline and was wondering how you can get it to search a directory by default for headers. Always inidcating them on the commandline with the -I dir can't be the only way?

Dragon - May 30, 2004 09:07 PM (GMT)
Why don't you use a text editor such as Emacs to make the scripts?

myork - May 30, 2004 09:34 PM (GMT)
That depends on the error you are seeing.

1) The first line of a shell script must be the name of the interpretor that will execute the script. So the first line should be:

CODE
#!/bin/csh


Now here I am using the csh as my shell and assuming there is a copy in /bin (this is probably a safe assumption but check). Or change to the shell you want to use. Some common shells are:

ksh
csh
tcsh
sh
bsh

or even perl


2) Also you need to make sure that the executable flag is set on the file.

To see the flags on a file use ls -l
CODE
ls -l
-rwxrw-r--    1 myork    Developm      847 May 30 17:13 t1


The flags on this file are:
rwx: for the owner (int this case myork)
rw-: for members of the group (Developm)
r--: for everybody else

if you want to set the flags use chmod <flag> <fileName>

CODE
chmod g+x <fileName>

In this case I added execute permission for members of the group.



3) The file must be in your path (see environment variable PATH) or you must specify an absolute/relative path to the file. To execute the file t1 that is in the current directory (and the current directory is not in the path)

CODE
./t1



PS Do not add '.' to your path this is a security vunrability.

dr voodoo - May 30, 2004 09:45 PM (GMT)
Thanks myork I'll try that out.

QUOTE
Why don't you use a text editor such as Emacs to make the scripts?

Simple I want to know how to do it without.

myork - May 30, 2004 09:52 PM (GMT)

Forgot to mention:

First line of script:

CODE
#!/bin/csh


The '#!' are required as the first two bytes of the file.
Immediately following that is the full path name of the interpretor.

KTC - May 31, 2004 04:20 AM (GMT)
QUOTE
PS Do not add '.' to your path this is a security vunrability.
I know that is the case but don't actually know why. Can someone provide an explanation please just for my knowledge/curisoty?

myork - May 31, 2004 06:02 AM (GMT)


Its easier to explain by example.

A friend of mine wrote a shell script called ls and placed this in his home directory.

The idea was that if sombody changed into his home direcotry and typed ls it would execute his program. Luckily for him he was caught by a friendly professor.

There were also over a couple of problems with his script (like ls is usually built into the shell). It assumed that '.' was the first item in the path etc. But more sophisticated hackers know all the problems with this approach and can compensate.

There are a lot of suttelties to this type of attach but thats the basics.

Incubator - May 31, 2004 12:36 PM (GMT)
#!/bin/bash or whatever
isnt a must.
writing a shelmlscript without it works too, but you have to run it with

bash myscript

or whatever shell you use

FrozenKnight - May 31, 2004 12:52 PM (GMT)
QUOTE
#!/bin/bash
Sounds like your beating up a dumpster. :lol:

Incubator - May 31, 2004 01:08 PM (GMT)
bash = Bourne Again Shell (or something like that :P)

C-Man - May 31, 2004 01:19 PM (GMT)
C-Man Bash'es Incubator on the head with a Unix Manual :lol:

Incubator - May 31, 2004 01:35 PM (GMT)
already got one :P

C-Man - May 31, 2004 01:36 PM (GMT)
:lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol: :lol:

myork - May 31, 2004 02:41 PM (GMT)
QUOTE
#!/bin/bash or whatever
isnt a must.
writing a shelmlscript without it works too, but you have to run it with

bash myscript

or whatever shell you use


Ahh.
But technicall that is not a shell script. Thats just a file with text in it.
What you are doing is envoking bash and saying read your commands from this file as if I typed them.

By stiking the 'Hash Bang' ([#!] as the americans are want to say) in the file you are turning it from a text file into an executable.

But then again I am just nitpicking.




* Hosted for free by InvisionFree