1 / 40

Library functions

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

adina
Download Presentation

Library functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. ~/lib/libFunction.txt • cat ~lib/libFunction.txt • scope () #function name • { #function body • echo this is a library function • }

  3. ~/lib/libFunctionArg.txt • myEchoArg () • { • echo I hope you supplied an argument • echo $1 • echo $0 • }

  4. 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

  5. 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

  6. 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~

  7. type which whereis • type ls • type pwd • which • whereis

  8. 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"

  9. Expanding arguments 2 for arg in $* • do • echo $arg • done • for arg in "$@" • do • echo $arg • done

  10. Logical and if [ arg1 -a arg2 ] • then • echo 1 • else • echo 2 • fi

  11. Short circuit • if [ true ] && [ true ] • then • echo 1 • else • echo 2 • fi

  12. This does not work • if [ true && true ] • then • echo 1 • else • echo 2 • fi

  13. Logical or • if [ true -o true ] • then • echo 1 • else • echo 2 • fi

  14. Logical or • if [ true -o true ] • then • echo 1 • else • echo 2 • fi

  15. Short circuit • if [ true ] || [ true ] • then • echo 1 • else • echo 2 • fi

  16. This does not work • if [ true || true ] • then • echo 1 • else • echo 2 • fi

  17. Communication • mailx • mesg • write • talk

  18. 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

  19. 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

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

  21. 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

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

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

  24. 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)

  25. Tee –a (append) • echo hi | tee -a file1.txt • This will append • Question • How would you stop tee from printing to screen?

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

  27. No clobber - append • set –o noclobber • echo “even more” >| myfile.txt • echo >> myfile.txt • This still works.

  28. 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

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

  30. 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)

  31. What does this print on screen? • if true && true • then • echo it's true • else • echo it's false • fi

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

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

  34. 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)

  35. 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.

  36. Check sum • []$ echo hi | cksum • 1479881546 3 • []$ echo hi | cksum • 1479881546 3 • []$ echo Hi | cksum • 3000792507 3

  37. Path check 1 • pathchk -p my12345678901234567890.sh • pathchk: name `my12345678901234567890.sh' has length 25; exceeds limit of 14 • The –p is for portability.

  38. 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

  39. 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 {} \;

More Related