executable scripts. so far we have made scripts echo hello #for example and called it hello.sh run...

13
Executable scripts

Upload: hugo-ellis

Post on 18-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?

Executable scripts

Page 2: Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?

So far

• We have made scripts• echo hello #for example• And called it hello.sh• Run it as sh hello.sh• This only works from current directory?• We can do todays lecture in the lab!!!

Page 3: Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?

overview

• Use the file command (before and after)• Add #! to the start of shell script. • THIS IS NOT A COMMENT!!!• Change the permissions• Add it to the path (front/end/twice). • (and export it)• Which, type commands• What if two script paths.

Page 4: Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?

Choosing a shell

• #! /bin/ksh• Specifies the Korn shell• #! /bin/bash• Is the Bash shell• #! bin/perl• Forces the shell to execute perl• #! /bin/awk –f • I think you know this one

Page 5: Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?

Where to put scripts

• Make a dir /bin or /scripts in your home dir• Put a script there (mv command).• File hello.sh #will says it’s a text file• Add #! /bin/bash as the first line• File hello.sh #will say it’s a bash script

Page 6: Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?

File with pdf and ps

• [zlizjw1@unnc-cslinux ~]$ file canonical.pdf• canonical.pdf: PDF document, version 1.4• [zlizjw1@unnc-cslinux ~]$ file canonical.ps• canonical.ps: PostScript document text

conforming at level 3.0• [zlizjw1@unnc-cslinux ~]$

Page 7: Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?

Change permissions

• How do we find permissions?• What is the error if we try to execute it?• chmod 7XX (make it executable)• Now check permissions• We can now run it without “sh” or “bash”• Sh hello.sh• Now we can run it directly as • hello.sh # JUST LIKE ANY OTHER COMMAND

Page 8: Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?

Change PATH variable

• echo $PATH• /usr/kerberos/bin:/bin:/usr/bin:• What is the field separator???• This tells the system where to look for

executable files. • Add /scripts to end• PATH=“$PATH:~/scripts”• echo $PATH # to make sure!

Page 9: Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?

Which script

• $ scripts/hello.sh• hello from the scripts dir• $ scripts2/hello.sh• hello from the SECOND scripts dir• $ hello.sh• hello from the scripts dir• echo $PATH• /usr/kerberos/bin:~/scripts

Page 10: Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?

Can still run 2nd script

• I can still run the other script but I must refer to it expliclty.

• ~/scripts2/hello.sh• hello from the SECOND scripts dir• THE MESSAGE IS ; you should not add your

/bin or /script to the start of the PATH variable, because your files will dominate!

Page 11: Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?

Change PATH variable permanently

• Need to add it to one of your dot login files• export PATH (makes available to subshells)• ls .*bash* # to list file• To run the file• Either log out and log back in• Or • source (filename)

Page 12: Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?

source or .

• [zlizjw1@unnc-cslinux ~]$ source .bash_profile

• hello John, you have just run .bash_profile• [zlizjw1@unnc-cslinux ~]$ . .bash_profile• hello John, you have just run .bash_profile• [zlizjw1@unnc-cslinux ~]$

Page 13: Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?

Stdin stdout

• Important – if you write to and from standard in and out (i.e. keyboard and screen)

• Then you can be used in pipelines and redirected more flexibly.