Shell_Scripting.pdf

(259 KB) Pobierz
AICT High Performance Computing Workshop
Shell Scripting
With Applications to
HPC
Edmund Sumbar
research.support@ualberta.ca
Copyright © 2007 University of Alberta. All rights reserved
Shell Scripting
High performance computing environment (1)
batch job
head node
execution nodes
May 4, 2007
Copyright © 2007 University of Alberta. All rights reserved
2
create
delete
Copyright © 2007 University of Alberta. All rights reserved.
1
Shell Scripting
High performance computing environment (2)
• Portable Batch System (PBS)
• Batch job described by a PBS script
• PBS script is a shell script
• Therefore, a batch job is a shell script
• Also, shell scripts for utility purposes
May 4, 2007
Copyright © 2007 University of Alberta. All rights reserved
3
create
delete
Shell Scripting
What is a shell (1)
`
Internet
head node
desktop
May 4, 2007
Copyright © 2007 University of Alberta. All rights reserved
4
create
delete
Copyright © 2007 University of Alberta. All rights reserved.
2
Shell Scripting
What is a shell (2)
• ssh client (on desktop) connects to ssh server (on head node)
• User authentication (login)
• A “shell” program is launched on head node (login shell)
• Local ssh client displays the results from the shell
• Allows you to execute commands interactively on the head node
May 4, 2007
Copyright © 2007 University of Alberta. All rights reserved
5
create
delete
Shell Scripting
Two major shell program variants
• Bourne-like shells
sh
ksh
bash
• C-like shells
csh
tcsh
Login shell specified for each user account
• Look for your
CCID
in
/etc/passwd
echo $SHELL
May 4, 2007
Copyright © 2007 University of Alberta. All rights reserved
6
create
delete
Copyright © 2007 University of Alberta. All rights reserved.
3
Shell Scripting
Very little difference for basic interactive use
Non-interactive use
• Execute a sequence of commands contained in a file (script)
• More advanced usage
• Differences in shell syntax become important
Focus on the bash shell
Reference documentation
man bash
info bash
May 4, 2007
Copyright © 2007 University of Alberta. All rights reserved
7
create
delete
Shell Scripting
Example commands (* internal command)
cd*
cp
mv
rm
mkdir
ls
cat
diff
find
grep
read*
chmod
awk
ps
kill*
sleep
echo*
exit*
qsub
qstat
May 4, 2007
Copyright © 2007 University of Alberta. All rights reserved
8
create
delete
Copyright © 2007 University of Alberta. All rights reserved.
4
Shell Scripting
Types of commands
• Simple
ls ~/src > filelist
test -e mhd.c
• Pipeline (sequence of one or more simple commands)
cat mhd.c | grep MPI | grep -v Bcast
• List (sequence of one or more pipelines)
cd ~/src ; test -e mhd.c && cp mhd.c mhd2.c &
• Compound commands (act on lists)
if ! grep MPI mhd.c ; then echo serial code ; fi
for f in * ; do test –L $f && rm $f ; done
May 4, 2007
Copyright © 2007 University of Alberta. All rights reserved
9
create
delete
Shell Scripting
Three ways to execute a script
• If script syntax is appropriate for current shell, set executable bit
on the file (chmod), and run script file directly
• Run shell program with script file as argument
(bash
scriptfile)
• Specify the path to the shell program on the first line, set the
executable bit, and execute the script directly (#!/bin/bash same
effect as
/bin/bash scriptfile)
May 4, 2007
Copyright © 2007 University of Alberta. All rights reserved
10
create
delete
Copyright © 2007 University of Alberta. All rights reserved.
5
Zgłoś jeśli naruszono regulamin