¡¾ÇóÖú¡¿ÈçºÎʵÏÖ×Ö·û´®²»Æ¥ÅäʱµÄ²Ù×÷
cat file/mnt/d/ccc.doc
/getip.sh
/jojo/game.sh
/mnt/e/xxx.xls
/mnt/e/conf/httpd.conf
/ffff.gif
/db/aaaa.pic
ÏëÒª×öµ½Ò»µã:
1. Èç¹ûfile ÖÐûÓÐ×Ö·û´®string µÄ»°(²»Æ¥Åä), ÔòÔÚÎļþÄ©Ìí¼ÓÒ»ÐÐ, ÄÚÈÝΪstring;
2. Èç¹ûfile ÖÐÓÐ×Ö·û´®string µÄ»°(Æ¥Åä), Ôò°Ñ¸ÃÐиÄΪstring ok
ÇëÎÊÓÃsed ÃüÁî¸ÃÔõôд sed ÎÒûÓз½·¨
grep µÄÎÒµ½ÓÐ
[CODE]liangke@liangke-desktop:/tmp$ cat file
/mnt/d/ccc.doc
/getip.sh
/jojo/game.sh
/mnt/e/xxx.xls
/mnt/e/conf/httpd.conf
/ffff.gif
/db/aaaa.pic
liangke@liangke-desktop:/tmp$ grep 'mnt' file
/mnt/d/ccc.doc
/mnt/e/xxx.xls
/mnt/e/conf/httpd.conf
liangke@liangke-desktop:/tmp$ echo $?
0
liangke@liangke-desktop:/tmp$ grep 'mntd' file
liangke@liangke-desktop:/tmp$ echo $?
1
liangke@liangke-desktop:/tmp$ [/CODE] [code]
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] cat file
hello
world
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] cat file.sed
#!/bin/sed -f
/string/ {
s/string/string ok/
q
}
$ a\
string
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] cat file.sed
#!/bin/sed -f
/string/ {
s/string/string ok/
q
}
$ a\
string
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] sed -i -f file.sed file ; cat file
hello
world
string
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] sed -i -f file.sed file ; cat file
hello
world
string ok
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] o
[/code] file.sed ÕâÑùдҲÐÐ:
[code]
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] cat file.sed
#!/bin/sed -f
s/string/string ok/
t done
$ a\
string
:done
[color=blue]-(dearvoid@LinuxEden:tty3)-(~/tmp)-
[19341 0] #[/color] o
[/code] ÁÁÐÖû´ïµ½LZÒªÇ󣬺Ç
dearvoidµÄ´úÂëÖ»ÔÚÖ»Óе¥ÐÐstringʱҲ»á¹¤×÷Õý³£
ÔÚÖ»ÔËÐÐ1£¬2´ÎʱҲÕý³£
[code]
[0 No.2058 huan@huan ~/tmp]$ sed -i -f file.sed txt; cat txt
hello
world
string
[0 No.2059 huan@huan ~/tmp]$ sed -i -f file.sed txt; cat txt
hello
world
string ok
[0 No.2060 huan@huan ~/tmp]$ sed -i -f file.sed txt; cat txt
hello
world
string ok ok
[/code]
À´¸ö²»Âײ»ÀàµÄ£¬½áºÏÂ¥ÉÏÁ½¸ö´ó´óµÄÏë·¨
ÀÄÓà &&, || ÁË£º£©
[code]
[0 No.2098 huan@huan ~/tmp]$ cat txt
hello
world
[0 No.2099 huan@huan ~/tmp]$ grep -q 'string.*ok' txt || { grep -q 'string' txt && sed -i 's/\(string\)/\1 ok/' txt || echo 'string' >> txt; }; cat txt
hello
world
string
[0 No.2100 huan@huan ~/tmp]$ grep -q 'string.*ok' txt || { grep -q 'string' txt && sed -i 's/\(string\)/\1 ok/' txt || echo 'string' >> txt; }; cat txt
hello
world
string ok
[0 No.2101 huan@huan ~/tmp]$ grep -q 'string.*ok' txt || { grep -q 'string' txt && sed -i 's/\(string\)/\1 ok/' txt || echo 'string' >> txt; }; cat txt
hello
world
string ok
[0 No.2102 huan@huan ~/tmp]$
[/code]
µ±È»
Èç¹ûÂ¥Ö÷˵Ã÷ÓзûºÏ '^string$' ÕâÑùµÄÐÐ
»á¼òµ¥Ð© °¥£¬¸Õ²Åæ×ÅÅäÖÃubuntuÁË£¬ÌâÄ¿ÎÒ¶¼Ã»Óп´Íê¡£ ÖØ·µubuÀ²£¿
д¸öÐĵÃ
·¢ÉÏubuntu/debian°æÀïßÂ
ÎÒÏÖÔÚ¶¨ÖÆubuntu edgy
ͬʱÔÚдÎĵµ
¹ýÒ»ÕóÌùÉÏÀ´ ×Ô¼ºÃ»ÓÐÃþË÷ʲô¶«Î÷£¬Òò´ËûÓÐÐĵÃ:rolleyes:
ÏÖÔÚÀíÂÛÉÏÊÇÔÚlinuxÏ£¬Êµ¼ÊÔÚwindowsÏÂ
ubu ºÜ²»´íÁË£¬ff Ò²¿ìÁË£¬¿Éϧff ²»ÊǺÜÎȶ¨£¬ÓÚÊÇÐéÄâÁËÒ»¸ödeepxin xp£¬×ªÁ˸öȦȦ¡£:D °³Ö»ÊÇÌṩµã¶ù˼·£¬²»ÊÇÍêÕûµÄ½â¾ö·½°¸ :) ·Ç³£¸Ðл¼¸Î»´ó´ó£¬ ÎÒÒªµÄ¾ÍÊÇ˼·¡£
¶ÔÁË£¬ »¹ÓÐÒ»¸öÎÊÌâÇë½Ì£º
Èç¹ûÎÒÓÃC ³ÌÐòµ÷ÓÃSHELL£¬ ÏëÒªµÃµ½SHELL ³ÌÐò·µ»ØµÄ±äÁ¿Öµ£¬ ¸ÃÔõô´¦Àí£¿ ¿´¿´ system() ¸ú waitpid() Á½¸öº¯ÊýµÄ man page ¾ÍÇå³þÁË [QUOTE=dearvoid]¿´¿´ system() ¸ú waitpid() Á½¸öº¯ÊýµÄ man page ¾ÍÇå³þÁË[/QUOTE]
°³Àí½â´íÁË :(
[COLOR=Magenta]SHELL ³ÌÐò·µ»ØµÄ±äÁ¿Öµ[/COLOR]ÔõôÀí½â? ¾ÙÀýÀ´Ëµ£º
result = `grep string file | awk '{print $2}'` ÕâÌõSHELL ÃüÁî
ÎÒÔÚC³ÌÐòÖÐÊÇÕâôÓõģº
sprintf(cmd, "%s\n", "grep string file | awk '{print $2}'" ) ;
system(cmd);
ÎÒÏëÔÚC ³ÌÐòÖлñµÃ$result µÄÖµ£¬ ÓÐʲô°ì·¨£¿ µÃµÈdearvoid»Ø¸´ÁË
ÎÒÖ»»áÒ»µãBashºÍÒ»µãPerl
PerlÔÚÕÕÑùÒ²ÊÇÓà ``È¡·µ»ØÖµ
system()Ö»Êǵ÷Óò¢·µ»ØÍ˳ö״̬
[code]
huanlf@huanlf-desktop:~$ perl -e ' $a=`echo hello world`; print "$a\n" '
hello world
huanlf@huanlf-desktop:~$
[/code]
²»Á˽âC
ÁíÍâ
Èç¹ûÊDz»Í¬Ö÷Ìâ
ÕÕÀí¿ªÐÂÖ÷Ìâ·¢ÎÊ£º£© [QUOTE=oldwell_nirvana]¾ÙÀýÀ´Ëµ£º
result = `grep string file | awk '{print $2}'` ÕâÌõSHELL ÃüÁî
ÎÒÔÚC³ÌÐòÖÐÊÇÕâôÓõģº
sprintf(cmd, "%s\n", "grep string file | awk '{print $2}'" ) ;
system(cmd);
ÎÒÏëÔÚC ³ÌÐòÖлñµÃ$result µÄÖµ£¬ ÓÐʲô°ì·¨£¿[/QUOTE]
FYI:
There is no way for a child process to pass an environment variable back to its parent process. But you can get the output of a command. The "official" way to do that is to use the popen() function. ͨ¹ýpopen() ºÍдÁÙʱÎļþ½»»¥ÊÇûÓÐÎÊÌâµÄ¡£
ÎÒÏëÒ²¿ÉÄÜÊÇûÓÐÕÐÁË£¬ лл´ó´ó£¡ [QUOTE=oldwell_nirvana]ͨ¹ýpopen() ºÍдÁÙʱÎļþ½»»¥ÊÇûÓÐÎÊÌâµÄ¡£
ÎÒÏëÒ²¿ÉÄÜÊÇûÓÐÕÐÁË£¬ лл´ó´ó£¡[/QUOTE]
Õâ¸öʱºò scripting languages µÄÓÅÊÆ¾ÍÏÔÏÖ³öÀ´ÁË :) ²»¹ý Python ºÃÏñҲûÓÐ built-in µÄÕâÖÖ feature, Ò²Ö»ÄÜÓà popen() ºÙºÙ£¬Perl ...
Ò³:
[1]