+ All Categories
Home > Documents > Library functions

Library functions

Date post: 19-Mar-2016
Category:
Upload: adina
View: 54 times
Download: 2 times
Share this document with a friend
Description:
Library functions. We do not have to run everything as a script We may have an interesting function we make available. There is an analogy with Java  Just like a public library we can borrow from. Typically store these in a lib/ directory. ~/lib/libFunction.txt. cat ~lib/libFunction.txt - PowerPoint PPT Presentation
40
Transcript

Library functions

• We do not have to run everything as a script• We may have an interesting function we make

available.• There is an analogy with Java • Just like a public library we can borrow from.• Typically store these in a lib/ directory.

~/lib/libFunction.txt

• cat ~lib/libFunction.txt

• scope () #function name• { #function body• echo this is a library function• }

~/lib/libFunctionArg.txt

• myEchoArg ()• {• echo I hope you supplied an argument• echo $1• echo $0 • }

My script calls library functions.

• #! /bin/bash• # this defines the path to the function• . ~/lib/libFunction.txt • . ~/lib/libFunctionArg.txt• scope• myEchoArg thisone• myEchoArg thatone

sh libCalling.sh – the output.

• sh libCalling.sh• this is a library function• I hope you supplied an argument• thisone• libCalling.sh• I hope you supplied an argument• thatone• libCalling.sh

Do Aliases work in a script?

• $ ls• a.txt b.txt c.txt remove.sh

remove.sh~• $ rm a.txt• rm: remove regular empty file `a.txt'? y• $ sh remove.sh• $ ls• remove.sh remove.sh~

type which whereis

• type ls• type pwd• which • whereis

Expanding arguments 1

• # run this script with different arguments. • #sh argsExpand.sh a b c d• #sh argsExpand.sh a b 'c d'• #sh argsExpand.sh a b "c d"

Expanding arguments 2for arg in $*

• do• echo $arg• done

• for arg in "$@"• do• echo $arg• done

Logical and

if [ arg1 -a arg2 ]• then• echo 1• else• echo 2• fi

Short circuit

• if [ true ] && [ true ]• then• echo 1• else• echo 2• fi

This does not work

• if [ true && true ]• then• echo 1• else• echo 2• fi

Logical or

• if [ true -o true ]• then• echo 1• else• echo 2• fi

Logical or

• if [ true -o true ]• then• echo 1• else• echo 2• fi

Short circuit

• if [ true ] || [ true ]• then• echo 1• else• echo 2• fi

This does not work

• if [ true || true ]• then• echo 1• else• echo 2• fi

Communication

• mailx• mesg• write• talk

mailx

• $ mailx -s hi zlizjw1• Hi John Hope you are doing okay• press ctrl d to end message• Cc: zlizjw1• /usr/sbin/sendmail: Permission denied• $ mailx• No mail for zlizjw1

Mesg 1

• 'y' and 'n' options respectively allow and disallow write access to your terminal.

• the permission other users have to write to your terminal using the talk and write commands

Mesg 2 • $ mesg• is y• $ mesg n• $ mesg n• $ mesg• is n• $ mesg y• $ mesg• is y

write

• The correct syntax for the write command is:• write user [tty] message• $ write zlizjw1• hi there, fancy a coffee in 10 minutes?• Message from zlizjw1@unnc-

cslinux.nottingham.edu.cn on pts/20 at 16:27 ...• hi there, fancy a coffee in 10 minutes?• EOF

Wait 1

• echo start of first loop > output.txt• for i in 1 2 3 4 5 • do •• echo i = $i >> output.txt• done &

Wait 2

• #$! is the process ID of the last process• echo $!• lastprocessID=$!• #this will wait for that process to end. • wait $lastprocessID

Tee for two?

• echo hi (this goes to screen)• echo hi > file1.txt (this goes to file1.txt )• echo hi | tee file1.txt (sends to screen and

file1.txt)• echo hi | tee file1.txt file2.txt (sends to screen

and file1.txt file2.txt)

Tee –a (append)

• echo hi | tee -a file1.txt• This will append

• Question• How would you stop tee from printing to

screen?

answer

• This will stop the print to screen • { echo hi| tee file1.txt file2.txt;} > /dev/null

No clobber - append

• set –o noclobber• echo “even more” >| myfile.txt• echo >> myfile.txt• This still works.

Octal dump

• $ echo "a b c d" >| a.txt• $ od a.txt• 0000000 020141 020142 020143 005144• 0000010• Usually very long – so pipe into head• od file.txt| head

Octal dump

• -c will give the characters. • echo "a b c" > file.txt• $ od -c file.txt• 0000000 a b c \n• 0000006

nohup

• If you have a big program you do not want to stop when you log off?

• java bigProgram& • nohup java bigProgram &• (nohup = no hang up – like a telephone)

What does this print on screen?

• if true && true• then • echo it's true• else• echo it's false• fi

• if • true • false • then • echo "its true"• else• echo "its false"• fi

• if true false • then • echo "its true"• else• echo "its false"• fi

time

• $ time sleep 5• real 0m5.004s• (real time elapsed)• user 0m0.000s• (time spent processing user’s command)• sys 0m0.002s• (time spent by system, e.g. moving processes)

Check sum

• If you sender and reciever have a file, how can they be sure the file is not corrupted by noise.

• Checksum will generate a number (two), and if the numbers at the same then they can be confident that the message was not corrupted.

Check sum

• []$ echo hi | cksum• 1479881546 3• []$ echo hi | cksum• 1479881546 3• []$ echo Hi | cksum• 3000792507 3

Path check 1

• pathchk -p my12345678901234567890.sh• pathchk: name

`my12345678901234567890.sh' has length 25; exceeds limit of 14

• The –p is for portability.

Path check 2

• $ pathchk a/a.txt• [zlizjw1@unnc-cslinux LAB]$ cat a/a.txt• this• [zlizjw1@unnc-cslinux LAB]$ chmod 000 a• [zlizjw1@unnc-cslinux LAB]$ pathchk a/a.txt• pathchk: directory `a' is not searchable

rm -file

• How to remove a file with a dash at the start?• Unix/linux thinks it’s an option!!!• $ rm -a.txt• rm: invalid option -- a• Try `rm --help' for more information.• So lets try this• rm ./-a.txt• find ./ -name –a.txt -exec rm -f {} \;


Recommended