SumLines() { # iterating over stdin - similar to awk
local sum=0
local line=””
while read line ; do
sum=$((${sum} + ${line}))
done
echo ${sum}
}
SumLines < data_one_number_per_line.txt
log() { # classic logger
local prefix="[$(date +%Y/%m/%d %H:%M:%S)]: "
echo "${prefix} $@" >&2
}
log "INFO" "a message"
尽可能的把你的bash代码移入到函数里,仅把全局变量、常量和对“main”调用的语句放在最外层。
变量注解
Bash里可以对变量进行有限的注解。最重要的两个注解是:
local(函数内部变量)
readonly(只读变量)
# a useful idiom: DEFAULT_VAL can be overwritten
# with an environment variable of the same name
readonly DEFAULT_VAL=${DEFAULT_VAL:-7}
myfunc() {
# initialize a local variable with the global default
local some_var=${DEFAULT_VAL}
...
}
;;kernel.asm
bits 32 ;nasm directive - 32 bit
section .text
global start
extern kmain ;kmain is defined in the c file
start:
cli ;block interrupts
call kmain
hlt ;halt the CPU
;;kernel.asm
;nasm directive - 32 bit
bits 32
section .text
;multiboot spec
align 4
dd 0x1BADB002 ;magic
dd 0x00 ;flags
dd - (0x1BADB002 + 0x00) ;checksum. m+f+c should be zero
global start
extern kmain ;kmain is defined in the c file
start:
cli ;block interrupts
call kmain
hlt ;halt the CPU
$ lsusb
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 002: ID 045e:00cb Microsoft Corp. Basic Optical Mouse v2.0
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
## existing rules are flushed to start with a new set of rules ##
iptables -F
iptables -A INPUT -s A.A.A.A/X -p udp --dport 53 -j ACCEPT
iptables -A INPUT -s B.B.B.B/Y -p udp --dport 53 -j ACCEPT
iptables -A INPUT -s C.C.C.C/Z -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j DROP
## making the rules persistent ##
service iptables save
$ killall --version
killall (PSmisc) 22.20
Copyright (C) 1993-2012 Werner Almesberger and Craig Small
PSmisc comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it under
the terms of the GNU General Public License.
For more information about these matters, see the files named COPYING.
libc.so.6 is neededby yum-metadata-parser-1.1.2-16.el6.i686
libc.so.6(GLIBC_2.0)is needed by yum-metadata-parser-1.1.2-16.el6.i686
libc.so.6(GLIBC_2.1.3)is needed by yum-metadata-parser-1.1.2-16.el6.i686
libglib-2.0.so.0 is needed by yum-metadata-parser-1.1.2-16.el6.i686
libpthread.so.0 is needed by yum-metadata-parser-1.1.2-16.el6.i686
libpython2.6.so.1.0 is needed by yum-metadata-parser-1.1.2-16.el6.i686
libsqlite3.so.0 is needed by yum-metadata-parser-1.1.2-16.el6.i686
libxml2.so.2 is needed by yum-metadata-parser-1.1.2-16.el6.i686
libxml2.so.2(LIBXML2_2.4.30) is needed by yum-metadata-parser-1.1.2-16.el6.i686
[infinality-bundle]
Server = http://bohoomil.com/repo/$arch
[infinality-bundle-multilib]
Server = http://bohoomil.com/repo/multilib/$arch
[infinality-bundle-fonts]
Server = http://bohoomil.com/repo/fonts
// 程序 1: 使一个LED闪烁开和关
int led = 13; // name the LED in pin 13
void setup() {
pinMode(LED, OUTPUT); // tell Arduino the pin in question is an output
}
void loop() {
digitalWrite(LED, HIGH); // deliver 5V to LED
delay(1000); // wait a second
digitalWrite(LED, LOW); // deliver 0V to LED
delay(1000); // wait a second
}
// 程序 2: 使Arduino打印 “Hello World”到电脑上
void setup() {
Serial.begin(9600); // open a 9600 baud communication line to computer
}
void loop() {
Serial.println(“Hello”); // write the word “Hello”
delay(1000); // wait a second
Serial.println(“World”); // write the word “World”
delay(1000); // wait a second
}