75
Brief Introduction on Linux Related Technology KONG, Lei

Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Brief Introduction on Linux Related Technology

KONG, Lei

Page 2: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Contents

1. Useful clients under Windows & Linux

2. Command line utilitie

3. Bash scripts

Page 3: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Section I

Useful Tools under Windows & Linux

Page 4: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Clients under Windows for Remote Login

• Use PuTTY/SecureCRT as ssh client to login to the remote server. – PuTTY:

http://www.chiark.greenend.org.uk/~sgtatham/putty/

– SecureCRT: http://www.vandyke.com/products/securecrt/index.html

Page 5: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Clients under Windows for File Transfer

• Use WinSCP to do file transfer between local and remote computer – WinSCP http://winscp.net/

– SFTP (SSH File Transfer Protocol) , mostly used with SSH-2

– SCP (Secure Copy Protocol) , mostly used with SSH-1

Page 6: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Clients under Windows for X

• Use Xmanager as Xwindow Server for X11 display. – Xmanager: http://www.netsarang.com/

Page 7: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Other Useful Tools

• GNU Screen – http://www.gnu.org/software/screen/

• lftp – sophisticated command line

ftp/http client – http://lftp.yar.ru/

• GNU wget

– http://www.gnu.org/software/wget/

Page 8: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Section II

BASIC Useful Linux/Unix Tools

(Particular Thanks: ZHAO, Shuqi)

Page 9: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

GNU Text Utilities

• dos, unix, mac text file – dos2unix unix2dos mac2dos

• time, estimate the time for the jobs

– time someprog progparam

• economize on disk

– run_certain_program -o /dev/stdout | gzip -c > result.gz

Page 10: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

GNU Text Utilities

• Is the program with correct format? – cat -A filename

• edit the original file

– sed -i -e ‘/badline/d’ *.fa – perl -i.bak -npe ‘s/old/new/g’ *.fa

• Input special characters in bash(eg.

tab) – ctrl+v special_character

Page 11: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

sort

• -b --ignore-leading-blanks • -d --dictionary-order • -f --ignore-case • -i --ignore-nonprinting • -n --numeric-sort • -r --reverse • -k POS1[,POS2] --key=POS1[,POS2] • -s --stable • -t SEPARATOR --field-separator=SEPARATOR

• -u --unique

Page 12: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

sort example

• A sort key position may also have any of the option letters `Mbdfinr‘ appended to it, in which case the global ordering options are not used for that particular field.

• sort – sort -k5,6nr -k7,7

Page 13: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

tar

• <options> for tar – x to eXtract from an archive.

– c to Create an archive.

– t to lisT the contents of an archive.

– z to work with a gzipped (ending with .gz) archive. (GNU Tar only)

– j to work with a bzip2 (ending with .bz2) archive. (GNU Tar only)

• more general for -z and -j bash $ tar cf - path/to/dir/ | gzip -c > xxx.tar.gz bash $ tar cf - path/to/dir/ | bzip2 -c > xxx.tar.bz2

Page 14: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

find

• Syntax:

– find pathname-lists option expression;

• Examples:

– To print out the current pathname for file ‘myfile’:

– To print out the yes* files accessed in the last two days:

bash $ find . -name myfile -print

bash $ find . -name ‘yes*’ -atime +2 -print

Page 15: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

find

• 如何将查询的结果作为命令行参数

[kongl@soft tmp]$ cd find-example/ [kongl@soft find-example]$ >a >b >c >d >e >f [kongl@soft find-example]$ ls a b c d e f [kongl@soft find-example]$ find . -type f -exec mv {} {}.1 \; [kongl@soft find-example]$ ls a.1 b.1 c.1 d.1 e.1 f.1

Page 16: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Difference between .bash_profile and .bashrc

• Login with shell (ssh user@host) /etc/profile -> ~/.bash_profile -> ~/.bash_login -> ~/.profile #append to .bash_profile test -f ~/.bashrc && . ~/.bashrc

• Login without shell (ssh user@host cmd)

/etc/bash.bashrc -> ~/.bashrc

Page 17: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Special Shell Variables

• $# The number of arguments.

• $@ All arguments, as separate words.

• $* All arguments, as one word.

• $$ ID of the current process.

• $? Exit status of the last command.

• $0,$1,..$9,${10},${11}...${N} Positional parameters.

cbimgmt $ cat wrapper.sh exec program.real “$@” cbimgmt $ cat wrapper.sh WHAT‟S THE DIFFERENCE exec program.real $@

Page 18: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Bash test and comparison functions

• test, [ ], [[ ]], (( )), or if-then-else-fi

• arithmetic comparisons

-eq, -ne, -lt, -le, -gt, or -ge,

• String comparisons

=, !=, <, or >

cbimgmt $ test expr #= [ expr ] , return $? cbimgmt $ test 3 -gt 4 && echo true || echo false cbimgmt $ [[ ( -d "$HOME" ) && ( -w "$HOME" ) ]] && \ echo "home is a writable directory"

Page 19: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

SSH Related Tips • always type the user and host name?

[kongl@soft tmp]$ cat ~/.ssh/config Host soft Hostname 162.105.250.142 User kongl

Page 20: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

SSH Public Key Authentication

[kongl@soft ~]$ ls ~/.ssh/ config known_hosts [kongl@soft ~]$ ssh-keygen -t dsa Generating public/private dsa key pair. Enter file in which to save the key (/home/kongl/.ssh/id_dsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/kongl/.ssh/id_dsa. Your public key has been saved in /home/kongl/.ssh/id_dsa.pub. The key fingerprint is: e4:b4:8d:98:f5:6f:39:5e:f7:96:f8:0c:c0:d6:cf:73 kongl@soft [kongl@soft ~]$ scp ~/.ssh/id_dsa.pub soft:.ssh/authorized_keys [email protected]'s password: id_dsa.pub 100% 600 0.6KB/s 00:00 [kongl@soft ~]$ ssh soft Last login: Sat Nov 28 15:34:57 2009 from 222.29.79.61

Page 21: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

SSH Copy without SCP I

[kongl@soft tmp]$ dd if=/home/kongl/tmp/c | ssh soft 'dd of=/home/kongl/tmp/e' 0+1 records in 0+1 records out 123 bytes (123 B) copied, 2.1e-05 seconds, 5.9 MB/s [email protected]'s password: 0+1 records in 0+1 records out 123 bytes (123 B) copied, 0.002105 seconds, 58.4 kB/s [kongl@soft tmp]$ ls a c d e find-example sort_example.txt test.sh [kongl@soft tmp]$ cat e Greg Sanders: All work and no play makes Greg a dull boy. Gil Grissom: All play and no work makes Greg an UNEMPLOYED boy.

Page 22: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

SSH Copy Without SCP II

[kongl@soft tmp]$ cat c | ssh soft 'cat > /home/kongl/tmp/f' [email protected]'s password: [kongl@soft tmp]$ ls a c d e f find-example sort_example.txt test.sh [kongl@soft tmp]$ cat f Greg Sanders: All work and no play makes Greg a dull boy. Gil Grissom: All play and no work makes Greg an UNEMPLOYED boy.

Page 23: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

lftp

• lftp [ftp://|http://|fish://]user@host • lftp -c command • lftp -e cmd • lftp -f script_file

• lftp commands

! shell command debug level|off cd, lcd get, put, mget, mput, pget mirror

Page 24: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Vim command mode operations

• :g/pattern/d (Delete lines with the specified pattern) • :%!sed /pattern/d (same as above) • :1,10s/old/new/g (Replace ‘old’ with ‘new’ in line 1 - 10

globally.) • :r!date (Read current date.) • :set nu, :set nonu (Display line number or not.) • :set ai, :set noai (auto indent) • :set hlsearch, :set nohlsearch (high light search results) • n+yy (copy n lines),p (paste),dd (delete/cut the current line),

u (undo) • w (move cursor forward a word),b (move cursor backward a

word) • shift+d (delete from cursor to line end) • ctrl+d (half page down),ctrl+u (half page up) • :w (save),:q! (force quit without saving),:wq (save & quit)

Page 25: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Vim insert mode

• i (insert mode)

• esc (quit insert mode)

• ctrl+v special_character (input a special character in insert mode)

Page 26: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Task Manager Tips

• crontab crontab –e

crontab filename

# use /bin/sh to run commands,

# no matter what /etc/passwd says

SHELL=/bin/sh

# run five minutes after midnight, every day

5 0 * * * $HOME/bin/daily.job &>> $HOME/tmp/out

# run at 2:15pm on the first of every month

15 14 1 * * $HOME/bin/monthly

Page 27: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Four device file

• /dev/stdin

• /dev/stdout

• /dev/stderr

• /dev/null

cbimgmt $ cat /etc/passwd |do_job -i /dev/stdin -o /dev/stdout

cbimgmt $ ln -s /dev/stdout o.png && convert in.ps o.png |display

cbimgmt $ find /etc –name „*‟ -type f -exec ls -l {} \; 2>/dev/null

Page 28: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Bash shortcuts – moving the cursor

Ctrl + a Go to the beginning of the line (Home) Ctrl + e Go to the End of the line (End) Ctrl + p Previous command (Up arrow) Ctrl + n Next command (Down arrow) Alt + b Back (left) one word Alt + f Forward (right) one word Ctrl + f Forward one character Ctrl + b Backward one character Ctrl + xx Toggle between the start of line and current cursor position

Page 29: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Bash shortcuts - editing Ctrl + L Clear the Screen, similar to the clear command

Ctrl + u Cut/delete the line before the cursor position.

Alt + Del Delete the Word before the cursor.

Alt + d Delete the Word after the cursor.

Ctrl + d Delete character under the cursor

Ctrl + h Delete character before the cursor (Backspace)

Ctrl + w Cut the Word before the cursor to the clipboard.

Ctrl + k Cut the Line after the cursor to the clipboard.

Alt + t Swap current word with previous

Ctrl + t Swap the last two characters before the cursor (typo).

Esc + t Swap the last two words before the cursor.

ctrl + y Paste the last thing to be cut (yank)

Alt + u UPPER capitalize every character from the cursor to the end of the current word.

Alt + l Lower the case of every character from the cursor to the end of the current word.

Alt + c Capitalize the character under the cursor and move to the end of the word.

Alt + r Cancel the changes and put back the line as it was in the history (revert).

ctrl + _ Undo

TAB Tab completion for file/directory names

Page 30: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Bash shortcuts - history

Ctrl + r Recall the last command including the specified character(s) searches the command history as you type. Equivalent to : vim ~/.bash_history. Ctrl + p Previous command in history (i.e. walk back through the command history) Ctrl + n Next command in history (i.e. walk forward through the command history) Ctrl + s Go back to the next most recent command. (beware to not execute it from a terminal because this will also launch its XOFF). Ctrl + o Execute the command found via Ctrl+r or Ctrl+s Ctrl + g Escape from history searching mode !! Repeat last command !abc Run last command starting with abc !abc:p Print last command starting with abc !$ Last argument of previous command ALT + . Last argument of previous command !* All arguments of previous command ^abc^def Run previous command, replacing abc with def

Page 31: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Bash shortcuts – process control

Ctrl + C Interrupt/Kill whatever you are running (SIGINT) Ctrl + l Clear the screen Ctrl + s Stop output to the screen (for long running verbose commands) Ctrl + q Allow output to the screen (if previously stopped using command above) Ctrl + D Send an EOF marker, unless disabled by an option, this will close the current shell (EXIT) Ctrl + Z Send the signal SIGTSTP to the current task, which suspends it. To resume the task enter ‘fg’ .

Page 32: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

screen related tips • create a screen: #screen [ENTER] • fork a new shell #screen [ENTER] or C-a c • name current window: C-a A • switch to certain window: C-a ‘ • Present a list of all windows: C-a “ • Send the command character (C-a): C-a a • detach screen: C-a d • Toggle to previously window: C-a C-a • To split screens: C-a S (Shift-s) • To unsplit screens: C-a Q (shift-q) • To switch between screens: C-a TAB • resumes a detached session: screen -r • Attach to a not detached session: screen -x

Page 33: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Section III

Bash Script Programming

Page 35: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

UNIX Philosophy

• KISS: Keep It Simple and Stupid

• OT2: One Time, One Thing

• “Collaboration makes the world better”

Page 36: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Very Simple Scripts

• Hello World

• A very simple backup script

[kongl@soft tmp]$ cat hello.sh #!/bin/bash echo Hello World [kongl@soft tmp]$ ./hello.sh Hello World

#!/bin/bash tar -cZf /var/my-backup.tgz /home/me/

Page 37: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Input & Output • Standard input (stdin): keyboard;

Standard output (stdout): monitor;

• A process has 3 special file descriptor.

Page 38: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Process file descriptor

$ ls -l /proc/14649/fd total 0 lrwx------ 1 lion lion 64 Aug 16 07:32 0 -> /dev/pts/8 lrwx------ 1 lion lion 64 Aug 16 07:32 1 -> /dev/pts/8 lrwx------ 1 lion lion 64 Aug 16 07:32 2 -> /dev/pts/8 Why did we look at directory /proc/14649/fd? /proc is for "process information" /14649 is for "process #14649", the running bash shell /fd is for "file descriptors", the list of the process' open file descriptors Now we see that we have three open file descriptors: 0,1,2. • 0 is stdin • 1 is stdout • 2 is stderr

Page 39: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

IO Redirection

• 在shell中,使用者可以利用“>”和“<”来进行输入输出重定向。

• command >file

– Redirect command’s stdout to a file

• command &>file

– Redirect command’s stdout to a file along with the stderr

• command >>file

– Append the command’s stdout to the file

• command &>> file

– Append the command’s stdout to the file along with the stderr

• command 2>&1

– Redirect stderr to stdout.

• command i>&j

– Redirect file descriptor i to j.

• j<>filename

– Open file ‘filename’, and assign file descriptor j to it.

Page 40: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

IO Redirection examples

[kongl@soft ~]$ > a ls [kongl@soft ~]$ cat a a Desktop F-7-i386-DVD.iso fedora7 gentoo new-svap surgemail_40u4_linux64.tar.gz tmp zimbra

Page 41: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

The order bash deal with io redirection: left to right

[kongl@soft tmp]$ ls [kongl@soft tmp]$ >a [kongl@soft tmp]$ >c [kongl@soft tmp]$ ls a c [kongl@soft tmp]$ rm b > d 2>&1 [kongl@soft tmp]$ cat d rm: cannot remove `b': No such file or directory [kongl@soft tmp]$ rm b 2>&1 >d rm: cannot remove `b': No such file or directory [kongl@soft tmp]$ cat d [kongl@soft tmp]$ [kongl@soft tmp]$ 2>&1 rm b > d rm: cannot remove `b': No such file or directory [kongl@soft tmp]$ >d 2>&1 rm b [kongl@soft tmp]$ >d rm b 2>&1 -----------------我是分隔符----------------- [kongl@soft tmp]$ rm b 2>&1 2>a [kongl@soft tmp]$ cat a rm: cannot remove `b': No such file or directory [kongl@soft tmp]$ rm b 2>a 2>&1 rm: cannot remove `b': No such file or directory [kongl@soft tmp]$ cat a [kongl@soft tmp]$

Page 42: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Redirection order test

[kongl@soft tmp]$ cat c Greg Sanders: All work and no play makes Greg a dull boy. Gil Grissom: All play and no work makes Greg an UNEMPLOYED boy. [kongl@soft tmp]$ cat c > a > d > e > f [kongl@soft tmp]$ cat a [kongl@soft tmp]$ cat d [kongl@soft tmp]$ cat e [kongl@soft tmp]$ cat f Greg Sanders: All work and no play makes Greg a dull boy. Gil Grissom: All play and no work makes Greg an UNEMPLOYED boy.

Page 43: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

More Funny

[kongl@soft tmp]$ cat test.sh #!/bin/bash cat c rm b [kongl@soft tmp]$ ls a c d test.sh [kongl@soft tmp]$ ./test.sh > a rm: cannot remove `b': No such file or directory [kongl@soft tmp]$ cat a Greg Sanders: All work and no play makes Greg a dull boy. Gil Grissom: All play and no work makes Greg an UNEMPLOYED boy. [kongl@soft tmp]$ ./test.sh > a 3>&1 1>&2 2>&3 3<&- Greg Sanders: All work and no play makes Greg a dull boy. Gil Grissom: All play and no work makes Greg an UNEMPLOYED boy. [kongl@soft tmp]$ cat a rm: cannot remove `b': No such file or directory

Page 44: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

File descriptor

[kongl@soft tmp]$ ls c d test.sh [kongl@soft tmp]$ cat c Greg Sanders: All work and no play makes Greg a dull boy. Gil Grissom: All play and no work makes Greg an UNEMPLOYED boy. [kongl@soft tmp]$ 3<>a cat c>&3 [kongl@soft tmp]$ cat a Greg Sanders: All work and no play makes Greg a dull boy. Gil Grissom: All play and no work makes Greg an UNEMPLOYED boy.

Page 45: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

PIPE

Process A Process B

STDOUT

STDERR

STDIN STDOUT

STDERR

Page 46: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Sample: simple pipe with sed

[kongl@soft tmp]$ ls a c d e f find-example hello.sh sort_example.txt test.sh [kongl@soft tmp]$ ls | sed -e "s/[aeio]/u/g" u c d u f fund-uxumplu hullu.sh surt_uxumplu.txt tust.sh

Page 47: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Sample: an alternative to ls -l *.txt

[kongl@soft tmp]$ ls -l *.txt -rw-r--r-- 1 kongl cbi 381 Nov 28 11:02 sort_example.txt [kongl@soft tmp]$ ls -l | grep "\.txt$" -rw-r--r-- 1 kongl cbi 381 Nov 28 11:02 sort_example.txt

Page 48: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Variables

• Sample: Hello World! using variables

• Sample: A very simple backup script (little bit better)

#!/bin/bash STR="Hello World!" echo $STR

#!/bin/bash OF=/var/my-backup-$(date +%Y%m%d).tgz tar -cZf $OF /home/me/

Page 49: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Local variables

[kongl@soft tmp]$ cat local_hello.sh #!/bin/bash HELLO=Hello function hello { local HELLO=World echo $HELLO } echo $HELLO hello echo $HELLO [kongl@soft tmp]$ ./local_hello.sh Hello World Hello

Page 50: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Conditionals

• if expression then statement

• if expression then statement1 else statement2

• if expression1 then statement1 else if expression2 then statement2 else statement3

Page 51: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Examples • Basic conditional example if .. then

• Basic conditional example if .. then ... else

• Conditionals with variables

#!/bin/bash if [ "foo" = "foo" ]; then echo expression evaluated as true fi

#!/bin/bash if [ "foo" = "foo" ]; then echo expression evaluated as true else echo expression evaluated as false fi

#!/bin/bash T1="foo" T2="bar" if [ "$T1" = "$T2" ]; then echo expression evaluated as true else echo expression evaluated as false fi

Page 52: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Loops - for [kongl@soft tmp]$ cat for_example.sh #!/bin/bash for i in $( ls *.*); do echo item: $i done echo "============separater==========" for i in `seq 1 5`; do echo $i done [kongl@soft tmp]$ ./for_example.sh item: for_example.sh item: hello.sh item: local_hello.sh item: sort_example.txt item: test.sh ============separater========== 1 2 3 4 5

Page 53: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Loops - while

[kongl@soft tmp]$ cat while_example.sh #!/bin/bash COUNTER=0 while [ $COUNTER -lt 5 ]; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done [kongl@soft tmp]$ ./while_example.sh The counter is 0 The counter is 1 The counter is 2 The counter is 3 The counter is 4

Page 54: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Loops - until

[kongl@soft tmp]$ cat until_example.sh #!/bin/bash COUNTER=10 until [ $COUNTER -lt 5 ]; do echo COUNTER $COUNTER COUNTER=`expr $COUNTER - 1` done [kongl@soft tmp]$ ./until_example.sh COUNTER 10 COUNTER 9 COUNTER 8 COUNTER 7 COUNTER 6 COUNTER 5

Page 55: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Functions without parameter

[kongl@soft tmp]$ cat fun_no_param.sh #!/bin/bash function quit { exit } function hello { echo "Hello World!" } hello quit echo "It should not reach here!" [kongl@soft tmp]$ ./fun_no_param.sh Hello World!

Page 56: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Functions with parameters [kongl@soft tmp]$ cat fun_with_param.sh #!/bin/bash function quit { exit } function my_echo { echo $1 } my_echo Hello World my_echo "Hello World" quit echo "It should not reach here, either!" [kongl@soft tmp]$ ./fun_with_param.sh Hello Hello World

Page 57: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Using select to make simple menus [kongl@soft tmp]$ cat select_menu.sh #!/bin/bash OPTIONS="Hello Test Quit" select opt in $OPTIONS; do if [ "$opt" = "Quit" ]; then echo done exit elif [ "$opt" = "Test" ]; then echo "This is a test." elif [ "$opt" = "Hello" ]; then echo "Hello World!" else clear echo "bad options!!!" fi done [kongl@soft tmp]$ ./select_menu.sh 1) Hello 2) Test 3) Quit #? 1 Hello World! #? 2 This is a test. #? 3 done

Page 58: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Using the command line parameters

[kongl@soft tmp]$ cat cmd_param.sh #!/bin/bash if [ -z "$1" ]; then echo usage: $0 directory exit fi SRCD=$1 TGTD="$HOME/tmp/" BASENAME=`basename $SRCD` OF=$BASENAME-$(date +%Y%m%d).tgz echo tar czf "$TGTD/$OF" $SRCD tar czf "$TGTD/$OF" $SRCD [kongl@soft tmp]$ ./cmd_param.sh usage: ./cmd_param.sh directory [kongl@soft tmp]$ ./cmd_param.sh find-example/ tar czf /home/kongl/tmp//find-example-20091129.tgz find-example/ [kongl@soft tmp]$ ls a d find-example fun_no_param.sh local_hello.sh test.sh c e find-example-20091129.tgz fun_with_param.sh select_menu.sh until_example.sh cmd_param.sh f for_example.sh hello.sh sort_example.txt while_example.sh

Page 59: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Reading user input with read

[kongl@soft tmp]$ cat read_example.sh #!/bin/bash echo Please, enter your name read NAME echo "Hi $NAME!" echo Please, enter your firstname and lastname read FN LN echo "Hi! $LN, $FN !" [kongl@soft tmp]$ ./read_example.sh Please, enter your name Kidding Hi Kidding! Please, enter your firstname and lastname Kidding Serious Hi! Serious, Kidding !

Page 60: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Arithmetic evaluation [kongl@soft tmp]$ cat arithmetic.sh #!/bin/bash echo "echo 1 + 1" echo 1 + 1 echo "echo \$((1+1))" echo $((1+1)) echo "echo \$[1+1]" echo $[1+1] echo "echo \$[3/4]" echo $[3/4] echo "echo 3/4|bc -l" echo 3/4|bc -l [kongl@soft tmp]$ ./arithmetic.sh echo 1 + 1 1 + 1 echo $((1+1)) 2 echo $[1+1] 2 echo $[3/4] 0 echo 3/4|bc -l .75000000000000000000

Page 61: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Getting the return value of a program

[kongl@soft tmp]$ cat return_val.sh #!/bin/bash cd /dada &> /dev/null echo return value: $? cd $(pwd) &> /dev/null echo return value: $? [kongl@soft tmp]$ ./return_val.sh return value: 1 return value: 0

Page 62: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Capturing a commands output

[kongl@soft tmp]$ cat capture_output.sh #!/bin/bash LIST=`ls find-example` for f in $LIST do echo FILE: $f done [kongl@soft tmp]$ ./capture_output.sh FILE: a.1 FILE: b.1 FILE: c.1 FILE: d.1 FILE: e.1 FILE: f.1

Page 63: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

DEBUG • The first line like this will produce some intresting output

information

• Example:

#!/bin/bash -x

[kongl@soft tmp]$ cat debug.sh #!/bin/bash -x PARAM=$1 echo $0 echo $1 [kongl@soft tmp]$ ./debug.sh test.sh + PARAM=test.sh + echo ./debug.sh ./debug.sh + echo test.sh test.sh

Page 64: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Acknownlegement

Dr. ZHAO, Shuqi

Page 65: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

REFERENCE

1. http://tldp.org/LDP/abs/html/io-redirection.html

2. http://www.linuxsir.org/main/doc/abs/abs3.7cnhtm/ioredirintro.html

3. http://hi.baidu.com/lb_hb/blog/item/bfe6a4659e8877f6f73654bd.html

4. http://www.diybl.com/course/3_program/shell/shelljs/200876/130761.html

5. http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

6. http://ss64.com/bash/syntax-keyboard.html

Page 66: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Thanks

Page 67: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents
Page 68: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Some Linux Tips

• mkfifo, a program to create named pipe

Page 69: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

GNU Text Utilities

• dos, unix, mac text file – dos2unix unix2dos mac2dos

• time, estimate the time for the jobs

– time someprog progparam

• economize on disk

– run_certain_program -o /dev/stdout | gzip -c > result.gz

Page 70: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

/proc directory organization • /proc/cpuinfo • /proc/meminfo • /proc/filesystems • /proc/version • /proc/[0-9]+ • /proc/[0-9]+/cmdline • /proc/[0-9]+/environ • /proc/[0-9]+/fd • /proc/[0-9]+/cwd

Page 71: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Some Linux Tips

• export

• hash -r

• reset

• wc #the “\n” number, not the line number

• ldd

cbimgmt $ ldd /bin/bash linux-gate.so.1 => (0xffffe000) libncurses.so.5 => /lib/libncurses.so.5 (0xb7f6e000) libdl.so.2 => /lib/libdl.so.2 (0xb7f6a000) libc.so.6 => /lib/libc.so.6 (0xb7e50000) /lib/ld-linux.so.2 (0xb7fb8000)

Page 72: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

file copy related Tips

• copy certain file to a new directory

• Compress certain files

• work with rsync

cbimgmt $ mkdir target cbimgmt $ find src -name „*.class‟|grep -v secret| \ cpio -pd target

cbimgmt $find ../dir1/ | cpio -o --format=tar > test.tar

or cbimgmt $find ../dir1/ | cpio -o -H tar|gzip> test.tar.gz

cbimgmt $ mkdir target cbimgmt $ rsync --exclude="*.java" -av src target/

Page 73: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Some Linux Tips

• Remember the work path

• Reorder the file column and row

cbimgmt $ cd some_path …. cbimgmt $ … do some work … cbimgmt $ forget where to return?

cbimgmt $ pushd some_path cbimgmt $ … do some work … cbimgmt $ popd

cbimgmt $ cat data 1 2 3 4 5 6 7 8 9 10

cbimgmt $ cat data |tr ' ' '\n'|xargs -l5 cbimgmt $ cat data |tr ' ' '\n'|paste - - - - - 1 2 3 4 5 6 7 8 9 10

cbimgmt $ cat data|tr ' ' '\n'|xargs -l3|tr ' ' '+'|bc 15 40

Page 74: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

Some Linux Tips • look up or signal process

• display a tree of processes

pgrep [-flvx] [-d delimiter] [-n|-o] [-P ppid,...] [-g pgrp,...] [-s sid,...] [-u euid,...] [-U uid,...] [-G gid,...] [-t term,...] [pattern] pkill [-signal] [-fvx] [-n|-o] [-P ppid,...] [-g pgrp,...] [-s sid,...] [-u euid,...] [-U uid,...] [-G gid,...] [-t term,...] [pattern] cbimgmt $ pgrep -u root #= ps -ef | egrep '^root ' | awk '{print $2}' cbimgmt $ pkill -HUP syslogd

pstree [-a] [-c] [-h|-Hpid] [-l] [-n] [-p] [-u] [-Z] [-A|-G|-U] [pid|user]

Page 75: Brief Introduction on Linux Related Technology · 2014-12-01 · tar • for tar –x to eXtract from an archive. –c to Create an archive. –t to lisT the contents

SSH Related Tips ## Linux Laptop .ssh/config ## Host work HostName 66.35.250.203 User sporkey LocalForward 20000 192.168.0.66:80 LocalForward 22000 192.168.0.66:22 LocalForward 22139 192.168.0.8:139 Host workssh HostName localhost User donkey Port 22000 HostKeyAlias workssh

## Linux Laptop .ssh/config ## Host workssh HostName 192.168.0.66 ProxyCommand ssh [email protected] /usr/bin/nc -w 1 %h 22