1 / 36

Part 1: Basic Commands/Utilities

Part 1: Basic Commands/Utilities. Basic Utilities. ls , cat, echo ls -l e.g., regular file or directory, permissions, file size ls -a cat file1 file2 file3 … echo “hello world” > file echo $VAR, echo hello* echo –n “hello world”. Basic Utilities. more, less, head, tail head -5 file

zack
Download Presentation

Part 1: Basic Commands/Utilities

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. Part 1: Basic Commands/Utilities

  2. Basic Utilities • ls, cat, echo • ls -l • e.g., regular file or directory, permissions, file size • ls -a • cat file1 file2 file3 … • echo “hello world” > file • echo $VAR, echo hello* • echo –n “hello world”

  3. Basic Utilities • more, less, head, tail • head -5 file • tail -3 file • pwd, cd • cp, mv, rm, mkdir, rmdir • ‘-i’ option • ‘-r’ option: rm –r dir1 • rmdiran empty directory

  4. Basic Utilities • sort, wc, grep, |(pipe) • wc -l file, wc –c file • grep “keyword” file • grep -v • sort linux | head -5 • bzip2, gzip, tar (no compression)

  5. Questions ‘cp –r dir1 dir2’ makes dir2 identical to dir1. True / False Which of the following commands can display the value of the present working directory? • pwd • echo $PWD • echo $(pwd) • pwd | echo • pwd| cat

  6. Questions Utilities head and tail can display the first or last several lines of a file. Explain the function of “head -8 file | tail -4”.

  7. Part 2: File Systems

  8. Filename and Path • Hierarchical tree structure • Absolute path name • ls /home/it244/it244 • Relative path name • cd /home/it244 • ls it244 • ‘.’ ‘..’ ‘~’ • ‘*’ ‘?’ • echo hello*, echo ./.?a*

  9. Access Permissions • r, w, x (chmod) • chmoda+rw file • chmod a=rx file • chmod 644 file • Execute permission of a directory • A directory is a table of files (filename:meta) • Executable means searchable

  10. Access Permissions • Operations on book/book1 • ls book/book1 • book ( x ) • cat book/book1 • book1 ( r ), book ( x ) • rm book/book1 • book ( xw ), book1 ( w )

  11. Links • Hard link and soft link • Hard link cannot refer to a directory • Soft link can point to a nonexistent file $ ln -s hello hello.sl $ rm hello $ echo “new” > hello $ cat hello.sl new

  12. Questions Both hard links and symbolic links can refer to directories. True / False Assume there are the following files: $ls a b c abbcac abccbaacbabcd How many files are listed as the output of “echo a?*” :

  13. Questions Assume there is a directory ‘dir1’ containing one file ‘file1’. To rename ‘dir1/file1’ to ‘dir1/file2’ , what permissions are needed and why?

  14. Part 3: Bash/Shell Scripting

  15. Execute Commands • Foreground and background jobs • command &, CTRL+Z • difference between a suspended job and a background job, PID • jobs, ps, kill, bg, fg • Group commands • (cmd1; cmd2; cmd3)

  16. Redirection • Standard input, output, error output • cat file1 file2 > out 2>err • cat file1 file2 &> out • cat file1 file2 > out 2>&1 • Redirected output will not be displayed on the screen.

  17. Variables • System variables • $PATH, $PWD, $HOME • User defined variables • VAR=hello • export VAR=hello • declare; declare -x

  18. Variables • New shell for executing a script $cat script1 echo $VAR VAR=new echo $VAR $VAR=old;./script1;echo $VAR $VAR=new ./script1;echo $VAR $export VAR=old;./script1;echo $VAR

  19. Expansion/ Quote • Types • echo hello*; echo ~ • echo $VAR • echo $(pwd) • echo $((1+1)) • VAR=hello* • echo $VAR • Single quote suspends all expansions

  20. Alias • Short names • alias rm=‘rm –i’ • alias ll=‘ls –l’ • alias mypwd1=“echo pwd is $PWD” • alias mypwd2=‘echo pwd is $PWD’

  21. Control Flow if..then..fi, if..then..else, while test-command do commands done for loop-index in argument-list do commands done

  22. Control Flow • Conditions • test $# -eq 0 • [ -f “$1” ] • [ condition1 –a condition2 ], -o • [[ && ]], (( || )) • Exit status $? • if [ read <file $line ] …

  23. Control Flow • Boolean operators • Short circuiting $((a=2, b=2)) $((a++ || b--)) $echo $a $b $((--a>2 || b++)) $echo $a $b

  24. Questions If we execute the following sequence of commands in order, what are the values of variables a, b and the output AFTER executing each line? $((a=1, b=2)) $echo $((a-- + b++))a=_0_, b=_3_, output=_3_ $echo $((a++ + ++b)) a=_1_, b=_4_, output=_4_

  25. Questions If we execute the following sequence of commands in order, what are the values of variables a and b AFTER executing each line? $ ((a=0, b=1)) $ ((a++ || b--)) a=_1_, b=_0_ $ ((++b || ++a))a=_1_, b=_1_

  26. Variables • Arguments • $1-$n, $0, $#, $@ • set set a 123 hello echo $1 $2 $3 set $(date) • shift echo $@ shift echo $@ • $$, $?

  27. Variables • Undefined variables • ${name:-default}, ${name:=default}, ${name:?message} echo ${VAR:-hello} echo ${VAR:=world} echo ${VAR:=world} echo ${VAR:-hello}

  28. File I/O • exec exec 3 < infile exec 4 > outfile • read • read -p “Enter sth: ” input1 input2 • read <&3 line, read –u3 line • Determine the end of a file • $line has no value • ‘read’ returns false • Write to a file • echo “abc” >&4

  29. Expressions • Arithmetic • a=5;b=3;echo $((a%b)) • echo $((a+++3)) • Logic • cat abc && ls • echo $?

  30. Part 4: Perl/AWK

  31. Run a Program • Perl • perl simple.pl • perl –e ‘print “hi\n”’ • AWK • gawk -f simple file • gawk ‘/chevy/ {print}’ file Stand-alone scripts #!/usr/bin/perl #!/usr/bin/gawk -f

  32. Regular Expressions • Patterns • perl • ., \d, \D • *, + .* • “aged”=~/a..d/ • AWK • $1 ~ /a..d/ • Search and replace (perl) • s/\/usr//

  33. Regular Expressions • Brackets in perl • $str=“10 + 20” • $str=~/(\d+).*(\d+)/ • print $1+$2

  34. File I/O in perl • Read open ($infile, '<', “filename”); while ($line=<$infile>){ print $line; } $line=<> • Write $filename="./log"; open($outfile,'>',$filename); print $outfile "Hello world!\n";

  35. Questions • Assume there is a file file1 containing one line Col1 Col2 Col3 Which of the following commands outputs ‘Col2’ • $set $(cat file1); echo $2 • $gawk ‘{print $2}’ file1 • $perl-e '$line=<>; $line=~ s/.* (.*) .*//; print "$1\n"' < file1

  36. Questions • Read the following shell script and answer the questions $cat script1 #!/bin/bash exec 3<$1 exec 4<$2 exec 5>$3 while read line <&3 do echo “$line” >&5 done while read line <&4 do echo “$line” >&5 done • Q1. The script requires 3 filenames as arguments, e.g., • ./script1 file1 file2 file3 • Which files are input files and which are output files? • Q2. Inside the while-loop, why variable “$line” has to • be enclosed by double quotes?

More Related