Linux
This presentation is the property of its rightful owner.
Sponsored Links
1 / 109

Linux 环境中的程序设计基础 PowerPoint PPT Presentation


  • 68 Views
  • Uploaded on
  • Presentation posted in: General

Linux 环境中的程序设计基础. 使用 vi 编辑文件. vi 编辑器使用. vi editor 是一个全屏幕文本编辑器 所有的 Linux 版本都提供了这一编辑器 在 Linux 系统中使用 vi 来编写文本文件 vi 也是 Unix 世界里极为普遍的全屏幕文本编辑器. vi 的启动.

Download Presentation

Linux 环境中的程序设计基础

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Linux

Linux

vi


Linux

vi

  • vi editor

  • Linux

  • Linuxvi

  • viUnix


Linux

vi

vi [options] [filename]

vi~

/

+nn

+/expexp


Shell

shell

vishellshex1

vi shex1vi

ivi

clear

cal 1 98

cal 2 98

cal 3 98

<Esc>:vi

:wqvi


Linux

vi2

vi2

  • vi

    vi

  • 2

    ESC

    :


Linux

    • a

    • i

    • o

  • 2Insert

  • ESC/


Linux

vi

  • [Delete][backspace]


Linux

vi

  • qvi

    q!vi

    wqvi

    wq!vi

    w [filename]vi

    e <filename><>

    e! <filename><>


Linux

  • xDel

    dw

    ddddd

    d$

    dG

  • u


Linux

RR

r

~


Linux

yy

ccut

ppaste


Linux

  • vivi

    s / / /

After completionOnce I have completed it.


Shell 2

shell2

shell

  • shellshell shell

    $sh <shell> [<1> <2> ]

    $sh shex1


Linux

  • shellshellshell

    $ <shell> [<1> <2> ]

    $ chmod a+x shex1

    $ /home/user1/shex1

    $ ./shex1


Linux

  • [][]2

  • shell$0$1$2$910

  • <1><2>$1$2

  • $0shell


Linux

shellshex3

clear

cal $1 98

cal $2 98

cal $3 98

$ sh shex3 1 2 3

1$1,

2$2,

3$3


Shift

shift

  • 10shift

  • shift $1$2<2> <3>

  • shift$1$2<3> <4>


Linux

shellshex2

echo $0 $1 $2

shift

echo $0 $1 $2

# /root/shex2 yesterday today tomorrow

/root/shex2 yesterday today

/root/shex2 today tomorrow

#

$0shell/root/shex2


Shell1

Shell

  • shell

  • shellshellshellshell

  • shellshellshell


Shell2

Shell


Shell3

Shell


Shell4

Shell

  • PATH shell PATH

  • Linux

  • PATH

  • HISTSIZEPATHPS1PS2


Shell5

Shell

  • shellenv

env

  • echo

echo ${}


Linux

  • bash

  • bash PS1 $

  • bashPS2 >

  • PS1PS2

export

shell

export PS1=\t\w\$


Linux


Linux

bash

  • bash

  • shell bash

  • bash shell

  • bash

/etc/profile

~/.bash_profile

~/.bash_login

~/.profile

~/.bashrc


Linux

bash

  • /etc/profile

Linux bash/etc/profile bash root

  • /etc/profilebash

~/.bash_profile

~/.bash_login

~/.profile

/etc/profile


Linux

bash

  • bash ~/.bashrc

  • shell bash bash bash bash bash ~/.bashrc

bash ~/.bashrc


Linux

  • shell

variable=value


Linux

<> =<>

shell

shell

name1=MARY

name2="X Window"


Linux

echo $variable echo ${variable}

unsetvariable

set

myname=jypan

echo $myname

unset myname

echo $myname


Linux


Linux

shell shell

$PID


Linux

$<>

$

# name2=$name1

name1name2

# echo $name1

name1


Linux

$$$shell

# winner=david

# notice="The person who won is $winner."

# echo $notice

The person who won is david.


Linux

# winner=david

# notice='The name is in the $winner variable.'

# echo $notice

The name is in the $winner variable.


Linux

\$

# winner=david

# result="$winner won \$100.00"

# echo $result

david won $100.00.


Linux

unset <>

shellunset *shell

# unset name1


Linux

  • shell shell shell

  • export

  • shell shell shell shell shell ( ) shell ( )

  • DNA


Linux

exportshell

export [] []=[]

-f []

-n

-p shell


Linux

export variable=value

variable=value; export variable

export -n variable

export -p


Printf

printf

  • printf

printf format

printf C printf

printf "%-12.5f\n" 123.456

format%

flag

field width

precision

-:

+:

0:0

:


Printf1

printf

printf

format


Printf2

printf

printf "The number is: %.2f\n" 100

printf "%-20s|%12.5f|\n" "Joy" 10

printf "%-10d%010o%+10x\n" 20 20 20

printf "%6d\t%6o\"%6x\"\n" 20 20 20


Linux

  • shell

  • shell

  • $1$2 $9 10 ${10}


Linux


Shell6

shell

  • Shell

shellLinuxshell

  • Shell

  • Shell

  • Shell .sh

  • Shell

chmod+xscript_name

./script_name

shscript_name


Linux

  • Bash

  • 0 0


Linux

  • test

  • test

x=5; y=10

test $x-gt$y

echo $?

  • test

x=5; y=10

[$x-gt $y ]

echo $?


Linux

  • 2.x Bash

name=Tom

[ $name= [Tt]??]

echo $?

[[ $name= [Tt]??]]

echo $?


Linux

name=Tom; [ -z $name ];echo $?

name2=Andy; [ $name= $name2];echo $?


Linux

x=1; [ $x-eq1];echo $?

X

x=a; [ $x-eq1];echo $?


Linux

  • let

==!=>>=<<=

x=1; let"$x==1";echo $?

x=1; (($x+1>= 2 ));echo $?

  • let

  • let


Linux

x=1; name=Tom;

[ $x-eq1 a n $name];echo $?

X

[($x-eq1) a (n $name)];echo $?


Linux

x=1; name=Tom;

[[ $x-eq1 && $name = To?]];echo $?


Linux

  • test

man test


Linux

["$name"="" ]

[ !"$name" ]

["X${name}"!="X" ]


Linux

if

if expr1 # expr1 (0)

then #

commands1 # commands1

elifexpr2 # expr1 expr2

then #

commands2 # commands2

... ...# elif

else # else

commands4 # commands4

fi #if fi


Linux

  • elif0

  • else0 1

  • if fi

  • expr

  • commands shell : 0

  • if

ex4if.shchkperm.shchkperm2.shname_greptellmetellme2idcheck.sh


Linux

case

case exprin # exprin

pattern1) # exprpattern1

commands1 # commands1

;; # case

pattern2) # exprpattern2

commands2 # commands2

;; # case... ...#

*) # expr

commands # commands

;; # caseesac # caseesac


Linux

  • expr case

  • expr *) ( if else ) *)

  • pattern |

  • caseyes_no.sh


Linux

for

for variableinlist

# list

do #

commands #

done #

  • list ( )

  • for list

  • for


Linux

for

list list do done list list

forloop.shmybackup.sh

  • $* $@

greet.sh

  • in list

permx.sh tellme greet.sh / permx.sh *


While

while

while expr # expr

do # expr0while

commands #

done #

expr 0 donewhile expr expr 0


Shift1

shift

shift[n]

  • list

  • listwhile shift

doit.sh a b c d e f g h

shft.sh a b c d e f g h


Linux

test

shelltest

test <>

test0

test0


Linux

  • if-then-else

    • if<>

      • then<1>

      • else <2>

  • fi

if-then-fi

if<>

then<>

fi

test <>00

iffi


Linux

/etcprofile/home/homeprofileprofile is not exist!

if test -e /etc/profile

then cp /etc/profile /home

cat /etc/profile|more

else echo profile is not exist!

fi


Linux

shp3shp3Parameter is lost! ,shp3/homeFile not found!


Linux

if test -z $1 -z$1

then echo Parameter is lost!

else if test -e $1 $1

then cp $1 /home

else echo File not found!

cat $0 $0

fi

fi


Linux

shellshp6shp6value

13value is less than 3 and not less than 1;

3value is not less than 3;

value=xxvalue


Linux

value=$1

if test value -ge 1 -a value -lt 3

then echo value is less than 3 and not less than 1

fi

if test value -ge 3

then echo value is not less than 3

fi

echo value=$value


Linux

1shellshp10shp10 xx yyxxyy3


Linux

if test $k -eq 3

then cal 7 $1

cal 8 $1

cal 9 $1

fi

if test $k -eq 4

then cal 10 $1

cal 11 $1

cal 12 $1

fi

clear

k=$2

if test $k -eq 1

then cal 1 $1

cal 2 $1

cal 3 $1

fi

if test $k -eq 2

then cal 4 $1

cal 5 $1

cal 6 $1

fi


Linux

case

case<>in

1)<1>

;;

n) <n>

;;

*)<>

;;

esac

<>


Linux

for-in

for <> [in <>]

do

<>

done

<>


Linux

foruser0user1user910for1010

for i in 0 1 2 3 4 5 6 7 8 9

do mkdir user$i

done

ls -dl user?|more

for i in 0 1 2 3 4 5 6 7 8 9

do rm -rf user$i

done

ls -dl user?|more


Linux

(1)

(2)

**********************

*This is a shell program *

**********************

(3)/home1File not found!12for


Linux

clear

echo **********************

echo *This is a shell program *

echo **********************


Linux

if test -n $1 -a -n $2

then if test $1!=$2

then if test -e $1

then mv $1 $2

for cn in ls -lcat

do

$cn $2

echo

done

else echo File not found!

fi

fi

fi


Linux

while

while <>

do

<>

done

<>0dodone<><>0done


Linux

until

until <>

do

<>

done

while <>0<>0done


Shell7

shell

shell

1set -v

shell

2set -x

shell


Linux1

Linux

Linux


Linux c c

Linux C/C++

  • CUnix

  • C

  • CLinuxC

  • ANSI CC

    • CUnix

    • C


Linux

  • 15<ctype.h><errno.h><float.h><math.h><stddef.h>I/O<stdio.h><stdlib.h><string.h><time.h><stdarg.h><signal.h><setjmp.h><local.h><assert.h>


Linux

C

  • LinuxCGNU CGNU CLinuxC

    • glibc

    • gcc

    • glibc_header

  • glibcCLinuxC

  • glibc_header,C


Linux

  • glibc

    • C

  • gccgccCC++

  • Linuxgcc


Linux

  • gcc


Linux

LinuxgccGNU C CompilerCC++Objective-CgccC

1

gcccpp #define#include

2

gccccl

3

gccas.o

4

gcc ld


Linux

C

  • c

  • libmX-WindowXliblibX11


Linux

  • C-libc/usr/lib


Linux

  • ELFa.outLinuxELF


Linux

  • /usr/lib/liblib.so.a.saa.out

    • .so..

    • .a

      libm.so.5m5

      libm.a


Linux

/lib

/usr/lib

/usr/i486-linux-libc5/liblibc5

/usr/X11R6/libX11R6

/usr/local/lib

/usr/include

/usr/local/include

/etc

ld.so.conf

ldconfig

ldd


Linux

gcc

gcc [options] [filename]

-ansi ANSI C GNU C asm typeof

-c

-DMACRO 1 MACRO

-DMACRO=DEFN DEFN MACRO

-E


Linux

-ggdb

-o <><>

-Ss

-O

-lobjco

-w

-Wall


Main c factorial c

main.c factorial.c

/*main.c*/

#include <stdio.h>

#include <stdlib.h>

int factorial (int n);

int main (int argc, char **argv)

{

int n;

if (argc < 2)

{

printf ("Usage: %s n\n", argv [0]);

return -1;

}

else

{

n = atoi (argv[1]);

printf ("Factorial of %d is %d.\n", n, factorial (n));

}

return 0;

}

/*factorial.c*/

#include <stdio.h>

#include <stdlib.h>

int factorial (int n)

{

if (n <= 1)

return 1;

else

return factorial (n - 1) * n;

}

$ gcc -o factorial main.c factorial.c

$ ./factorial 5

Factorial of 5 is 120.


Linux

  • GCC C C++ C C C+ +

  • Linux C .c C++ .C .cpp gcc C++ C++ g++ C++ gcc

  • C++ hello.C

    #include <iostream.h>

    void main (void)

    {

    cout << "Hello, world!" << endl;

    }

$ g++ -o hello hello.C

$ ./hello

Hello, world!


Linux

C

  • LinuxgdbGNU debugger

  • GNU gdb X Window gdb xxgdb

  • gdb


Linux

  • gdb

  • gdb -g makefile CFLAGS

    CFLAGS = -g

  • gdb

    #gdb progname


Linux

gdb help

  • aliases

  • breakpoints

  • data

  • files

  • internals

  • running

  • stack

  • statu

  • tracepoints

    help


Linux

gdb


Linux

gdbgcc g


Linux

star.c

**********

#include <stdio.h>

int main()

{

int i=10

do

{

printf*

++i

}whilei<10

printf\n

}

}

star.cstargdb


Linux

#gcc -o star -g star.c

#gcc -g star.c -o star

#./star

*

gdb

#gdb ./star

GNU gdb Red Hat Linux

(gdb)run

*

(gdb)where

No stack


Linux

(gdb)list

1#include <stdio.h>

2int main()

3{

4int i=10

5do

6{

7printf*

8++i

9 }whilei<10

10 printf\n

(gdb)break 1

Breakpoint 1 at 0x8048370:file star.c,line 1.

(gdb)run

Breakpoint 1,main() at star.c:3

3{


Linux

(gdb)step

7printf(*);

(gdb)print i

$1=7146752

ii

gdb4

(gdb)clear 1

Deleted breakpoint 1

(gdb)break 4

Breakpoint 2 at 0x8048380:file star.c,line 4.


Linux

(gdb)run

The program being debugged has been started already.

Start it from the beginning?(y or n) y

Starting program:/root/star

Breakpoint 2,main() at star.c:4

4int i=10;

(gdb)step

7printf(*);

(gdb)print i

$2=10

(gdb)set var i=0

(gdb)print i

$3=0

(gdb)continue

**********


  • Login