Functions to be implemented |
C Programming |
Linux Shell Script Programming |
Passing Program/Script Parameters |
Int main (INT argc, char ** argv) { If (argv! = 4 ){ Printf ("Usage: % s arg1 arg2 arg3", argv [0]); Return 1; } Printf ("arg1: % s \ n", argv [1]); Printf ("arg2: % s \ n", argv [2]); Printf ("arg3: % s \ n", argv [3]); Return 0; } |
#! /Bin/sh If [$ #-lt 3]; then Echo "Usage: 'basename $ 0' arg1 arg2 arg3"> & 2 Exit 1 Fi Echo "arg1: $1" Echo "arg2: $2" Echo "arg3: $3" Exit 0 |
Int main (INT argc, char ** argv) { Int I; For (I = 1; I <= argc; I ++ ){ Printf ("Arg: % s \ n", argv [I]); } Return 0; } |
#! /Bin/sh While [$ #-Ne 0] Do Echo "Arg: $1" Shift Done |
Logical/numeric operations |
If (D = 0) |
If ["$ D"-EQ "0"]; then |
If (D! = 0) |
If ["$ D"-ne "0"]; then |
If (D> 0) |
If ["$ D"-GT "0"]; then |
If (d <0) |
If ["$ D"-lt "0"]; then |
If (d <= 0) |
If ["$ D"-Le "0"]; then |
If (D> = 0) |
If ["$ D"-ge "0"]; then |
String comparison |
If (strcmp (STR, "ABC") = 0 ){ } |
If ["$ Str "! = "ABC"]; then Fi |
Input and Output |
Scanf ("% d", & D ); |
Read d |
Printf ("% d", d ); |
Echo-N $ d |
Printf ("% d", d ); |
Echo $ d |
Printf ("press any to continue ..."); Char CH = getchar (); Printf ("\ Nyou pressed: % C \ n", CH ); |
#! /Bin/sh Getchar () { Savedtty = 'stty-G' Stty cbreak Dd If =/dev/tty BS = 1 COUNT = 1 2>/dev/null Stty-cbreak Stty $ savedtty } Echo-n "press any key to continue ..." Ch = 'getchar' Echo "" Echo "you pressed: $ ch" |
|
Read d <& 3 |
Procedures for controlling programs and scripts |
If (isok ){ // 1 } Else if (isok2 ){ // 2 } Else { // 3 } |
If [isok]; then #1 Elif [isok2]; then #2 Else #3 Fi |
Switch (d) { Case 1: Printf ("you select 1 \ n "); Break; Case 2: Case 3: Printf ("you select 2 or 3 \ n "); Break; Default: Printf ("error \ n "); Break; }; |
Case $ D in 1) echo "you select 1" ;; 2 | 3) echo "you select 2 or 3" ;; *) Echo "error" ;; Esac |
For (INT loop = 1; loop <= 5; loop ++ ){ Printf ("% d", loop ); } |
For Loop in 1 2 3 4 5 Do Echo $ Loop Done |
Do { Sleep (5 ); } While (! Isroot ); |
Is_root = 'who | grep root' Until ["$ is_root"] Do Sleep 5 Done |
Counter = 0; While (counter <5 ){ Printf ("% d \ n", counter ); Counter ++; } |
Counter = 0 While [$ counter-lt 5] Do Echo $ counter Counter = 'expr $ counter + 1' Done |
While (1 ){ } |
While: Do Done |
Break; |
Break or break N, N indicates that the n-level loop exists. |
Continue; |
Continue |
Function and Process Definition |
Void Hello () { Printf ("Hello \ n "); } ... // Function call Hello (); |
Hello () { Echo "Hello" } Or Function Hello () { Echo "Hello" } ... # Function call Hello |
Function parameters and return values |
Int ret = doit (); If (ret = 0 ){ Printf ("OK \ n "); } |
Doit If ["$ ?" -EQ 0]; then Echo "OK" Fi Or Ret = doit If ["$ RET"-EQ "0"]; then Echo "OK" Fi |
Int sum (int A, int B) { Return A + B; } Int S = sum (1, 2 ); Printf ("the sum is: % d \ n", S ); |
Sum () { Echo-n "'expr $1 + $2 '" } S = 'sum 1 2' Echo "the sum is: $ S" |
Bool isok () {return false ;} If (isok ){ Printf ("Yes \ n "); } Else { Printf ("No \ n "); } |
Isok () { Return 1; } If isok; then Echo "yes" Else Echo "no" Fi |