The functionality to implement
|
C Language Programming
|
Linux Shell Script Programming
|
Parameter passing of program/script
|
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 [$#- Span>lt 3]; Then
echo Usage: ' basename $ ' arg1 arg2 arg3 ' >&2
exit 1
fi
echo "Arg1: $"
echo "arg2: $"
echo "Arg3: $"
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
& nbsp; echo "ARG: $"
shift
do Ne
|
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 comparisons
|
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
|
program/script Control process
|
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 ("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
Todo
Echo $loop
Done
|
do {
Sleep (5);
while (! isRoot);
|
Is_root= ' who | grep Root '
Until ["$IS _root"]
Todo
Sleep 5
Done
|
counter=0;
while (Counter < 5) {
printf ("%d/n", counter);
counter++;
}
|
Counter=0
While [$COUNTER- LT 5]
Todo
Echo $COUNTER
Counter= ' expr $COUNTER + 1 '
Done
|
while (1) {
}
|
While:
Todo
Done
|
Break
|
Break or break n,n means jump out of n-level loops
|
Continue
|
Continue
|
Definitions of functions and procedures
|
void Hello ()
{
printf ("hello/n");
}
...
Function call
Hello ();
|
Hello ()
{
Echo "Hello"
} or
function Hello ()
{
Echo "Hello"
}
...
#函数调用
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 ()
{
&nb sp; echo-n "Expr $ + $"
}
s= ' sum 1 2 '
Ech O "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
|