终端命令行

Shell脚本编程最佳实践

Shell脚本是系统管理和自动化任务的利器。本文将带你从基础到高级,全面掌握Shell脚本编程的最佳实践。 Shell基础 第一个Shell脚本 #!/bin/bash # 这是一个注释 echo "Hello, World!" # 变量赋值和使用 name="World" echo "Hello, $name!" # 命令替换 current_date=$(date) echo "Today is: $current_date" # 反引号方式(不推荐) current_date=`date` echo "Today is: $current_date" Shebang说明: #!/bin/bash:使用bash解释器 #!/bin/sh:使用sh解释器(更通用) #!/usr/bin/env bash:自动查找bash(更便携) 变量和数据类型 # 字符串变量 greeting="Hello" name="Alice" # 只读变量 readonly PI=3.14159 # 删除变量 unset name # 环境变量 export PATH=$PATH:/new/path # 字符串拼接 fullname="John $greeting" echo $fullname # 获取字符串长度 string="Hello, World" echo ${#string} # 13 # 字符串切片 echo ${string:0:5} # Hello echo ${string:7} # World # 默认值 echo ${name:-"Guest"} # 如果name未设置或为空,使用"Guest" # 数组 arr=(apple banana cherry) echo ${arr[0]} # apple echo ${arr[@]} # 所有元素 echo ${#arr[@]} # 数组长度 arr[3]="date" # 添加元素 unset arr[1] # 删除元素 控制结构 条件判断 # if语句 if [ "$name" == "Alice" ]; then echo "Welcome, Alice!" elif [ "$name" == "Bob" ]; then echo "Welcome, Bob!" else echo "Welcome, Guest!" fi # 数字比较 count=10 if [ $count -eq 10 ]; then echo "Count is 10" fi if [ $count -gt 5 ]; then echo "Count is greater than 5" fi if [ $count -lt 20 ]; then echo "Count is less than 20" fi # 字符串比较 if [ "$string1" == "$string2" ]; then echo "Strings are equal" fi if [ -n "$string" ]; then echo "String is not empty" fi # 文件测试 if [ -f "file.txt" ]; then echo "File exists and is a regular file" fi if [ -d "/tmp" ]; then echo "Directory exists" fi if [ -r "file.txt" ]; then echo "File is readable" fi if [ -w "file.txt" ]; then echo "File is writable" fi if [ -x "script.sh" ]; then echo "File is executable" fi # 逻辑运算 if [ $count -gt 5 ] && [ $count -lt 20 ]; then echo "Count is between 5 and 20" fi if [ $count -lt 5 ] || [ $count -gt 20 ]; then echo "Count is outside range 5-20" fi # 使用test命令 if test -f "file.txt"; then echo "File exists" fi # 双括号(更强大的算术比较) if (( count > 5 && count < 20 )); then echo "Count is between 5 and 20" fi 循环结构 # for循环 for i in 1 2 3 4 5; do echo $i done # 遍历文件 for file in *.txt; do echo "Processing: $file" done # C风格for循环 for ((i=0; i<10; i++)); do echo $i done # while循环 count=0 while [ $count -lt 5 ]; do echo $count count=$((count + 1)) done # 读取文件行 while IFS= read -r line; do echo "$line" done < file.txt # until循环 count=0 until [ $count -ge 5 ]; do echo $count count=$((count + 1)) done # break和continue for i in {1..10}; do if [ $i -eq 5 ]; then continue # 跳过5 fi if [ $i -eq 8 ]; then break # 在8处停止 fi echo $i done case语句 # 简单的case语句 read -p "Enter a color: " color case $color in red) echo "You chose red" ;; blue|green) echo "You chose blue or green" ;; *) echo "You chose something else" ;; esac # 复杂的case语句 case $1 in start) echo "Starting service..." ;; stop) echo "Stopping service..." ;; restart) echo "Restarting service..." ;; status) echo "Checking service status..." ;; *) echo "Usage: $0 {start|stop|restart|status}" exit 1 ;; esac 函数编程 定义和使用函数 # 定义函数 greet() { echo "Hello, $1!" } # 调用函数 greet "Alice" # 返回值 add() { local result=$(($1 + $2)) echo $result } sum=$(add 5 3) echo "Sum: $sum" # 返回状态码 check_file() { if [ -f "$1" ]; then return 0 # 成功 else return 1 # 失败 fi } if check_file "file.txt"; then echo "File exists" else echo "File does not exist" fi # 局部变量 global_var="I am global" my_function() { local local_var="I am local" echo "Inside function: $local_var" echo "Inside function: $global_var" } my_function echo "Outside function: $global_var" # echo "Outside function: $local_var" # 错误:local_var未定义 函数参数 # 处理多个参数 process_args() { echo "First argument: $1" echo "Second argument: $2" echo "All arguments: $@" echo "Number of arguments: $#" echo "Script name: $0" } process_args arg1 arg2 arg3 # 遍历所有参数 iterate_args() { for arg in "$@"; do echo "Processing: $arg" done } iterate_args file1.txt file2.txt file3.txt # shift命令 shift_test() { echo "Total arguments: $#" echo "First: $1" shift echo "After shift, first: $1" echo "Remaining arguments: $#" } shift_test a b c d 递归函数 # 阶乘(尾递归) factorial() { local n=$1 local acc=${2:-1} if [ $n -le 1 ]; then echo $acc else factorial $((n - 1)) $((acc * n)) fi } echo "Factorial of 5: $(factorial 5)" # Fibonacci fibonacci() { local n=$1 if [ $n -le 1 ]; then echo $n else echo $(( $(fibonacci $((n - 1))) + $(fibonacci $((n - 2))) )) fi } echo "Fibonacci of 10: $(fibonacci 10)" 输入输出 读取用户输入 # 简单输入 read -p "Enter your name: " name echo "Hello, $name!" # 密码输入(不显示) read -s -p "Enter password: " password echo # 带超时的输入 read -t 5 -p "Enter your choice (5 seconds): " choice echo "You chose: $choice" # 读取多个值 read -p "Enter name age: " name age echo "Name: $name, Age: $age" # 从文件读取 while IFS= read -r line; do echo "Line: $line" done < input.txt # 读取确认 read -p "Continue? (y/n): " confirm if [[ $confirm == [yY] ]]; then echo "Continuing..." else echo "Aborting..." exit 1 fi 输出格式化 # echo选项 echo -n "No newline" # 不换行 echo -e "Line1\nLine2" # 解释转义字符 echo "Hello\tWorld" # 需要配合-e # printf格式化输出 printf "Name: %s, Age: %d\n" "Alice" 25 printf "Pi: %.2f\n" 3.14159 printf "%-10s %10s\n" "Left" "Right" # 重定向输出 echo "Error message" >&2 # 输出到stderr echo "Log message" >> logfile # 追加到文件 # 管道 echo "Hello World" | tr '[:upper:]' '[:lower:]' # Here文档 cat << EOF This is a multi-line string using Here document. EOF # Here字符串 grep "pattern" <<< "This is a string to search" 命令行参数 处理位置参数 #!/bin/bash # script.sh echo "Script name: $0" echo "First argument: $1" echo "Second argument: $2" echo "All arguments: $@" echo "Number of arguments: $#" # 检查参数数量 if [ $# -lt 2 ]; then echo "Usage: $0 <arg1> <arg2>" exit 1 fi 使用getopts #!/bin/bash # 使用getopts处理选项 usage() { echo "Usage: $0 [-a] [-b VALUE] [-c] filename" exit 1 } while getopts ":ab:c" opt; do case $opt in a) echo "Option -a triggered" ;; b) echo "Option -b triggered with value: $OPTARG" value=$OPTARG ;; c) echo "Option -c triggered" ;; \?) echo "Invalid option: -$OPTARG" usage ;; :) echo "Option -$OPTARG requires an argument" usage ;; esac done shift $((OPTIND-1)) echo "Remaining arguments: $@" 使用getopt(更强大) #!/bin/bash # 使用getopt处理长选项 TEMP=$(getopt -o ab:c:: --long alpha,bravo:,charlie:: -n 'example.sh' -- "$@") if [ $? != 0 ]; then echo "Terminating..." >&2 exit 1 fi eval set -- "$TEMP" while true; do case "$1" in -a|--alpha) echo "Option a" shift ;; -b|--bravo) echo "Option b, argument '$2'" shift 2 ;; -c|--charlie) case "$2" in "") echo "Option c, no argument" shift 2 ;; *) echo "Option c, argument '$2'" shift 2 ;; esac ;; --) shift break ;; *) echo "Internal error!" exit 1 ;; esac done echo "Remaining arguments:" for arg in "$@"; do echo " --> '$arg'" done 信号处理 捕获中断 #!/bin/bash # 捕获Ctrl+C cleanup() { echo "Cleaning up..." # 删除临时文件等 rm -f /tmp/my_script_temp* exit 1 } trap cleanup SIGINT SIGTERM echo "Press Ctrl+C to interrupt..." for i in {1..100}; do echo "Working... $i" sleep 1 done 捕获EXIT信号 #!/bin/bash # 确保清理代码总是执行 cleanup() { echo "Script is exiting..." rm -f /tmp/tempfile } trap cleanup EXIT # 创建临时文件 touch /tmp/tempfile echo "Doing some work..." # 即使脚本出错,cleanup也会执行 文本处理 文件操作 # 读取文件 while IFS= read -r line; do echo "$line" done < file.txt # 写入文件 echo "Hello" > output.txt echo "World" >> output.txt # 检查文件是否存在 if [ -f "file.txt" ]; then echo "File exists" fi # 检查文件是否可读 if [ -r "file.txt" ]; then echo "File is readable" fi # 获取文件大小 size=$(wc -c < file.txt) echo "File size: $size bytes" # 获取行数 lines=$(wc -l < file.txt) echo "File lines: $lines" 文本转换 # 转换为大写 echo "hello" | tr '[:lower:]' '[:upper:]' # 删除重复行 sort file.txt | uniq # 只显示重复行 sort file.txt | uniq -d # 统计重复次数 sort file.txt | uniq -c # 替换文本 sed 's/old/new/g' file.txt # 删除空行 sed '/^$/d' file.txt # 提取特定列 awk '{print $1, $3}' file.txt # 按模式分割文件 awk '/pattern/{filename="part_"++count".txt"; print > filename}' 进程管理 后台执行 # 后台运行 command & # 后台运行并重定向输出 command > /dev/null 2>&1 & # 使用nohup(退出终端后继续运行) nohup command & # 查看后台任务 jobs # 带回后台任务 fg %1 # 继续后台任务 bg %1 # 杀死后台任务 kill %1 进程监控 # 查看进程 ps aux # 查找特定进程 ps aux | grep nginx # 实时监控 top # 杀死进程 kill PID kill -9 PID # 强制杀死 # 等待进程完成 wait PID 调试技巧 调试模式 #!/bin/bash # 启用调试模式 set -x # 在执行前打印命令 set -v # 打印输入行 # 或者 bash -x script.sh # 只调试部分代码 set -x # 开始调试 # 需要调试的代码 set +x # 结束调试 错误处理 #!/bin/bash # 遇到错误立即退出 set -e # 使用未定义变量时报错 set -u # 管道命令失败时退出 set -o pipefail # 组合使用 set -euo pipefail # 捕获错误 trap 'echo "Error on line $LINENO"; exit 1' ERR 日志记录 #!/bin/bash # 日志函数 log() { local level=$1 shift echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$level] $@" | tee -a script.log } log INFO "Script started" log ERROR "An error occurred" log WARNING "This is a warning" 实用示例 系统监控脚本 #!/bin/bash # 系统监控脚本 while true; do clear echo "=== System Monitor ===" echo "Time: $(date)" echo # CPU使用率 echo "CPU Usage:" top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}' # 内存使用 echo -e "\nMemory Usage:" free -h # 磁盘使用 echo -e "\nDisk Usage:" df -h sleep 5 done 日志分析脚本 #!/bin/bash # Apache日志分析 log_file="/var/log/apache2/access.log" echo "=== Top 10 IPs ===" awk '{print $1}' "$log_file" | sort | uniq -c | sort -rn | head echo -e "\n=== Top 10 URLs ===" awk '{print $7}' "$log_file" | sort | uniq -c | sort -rn | head echo -e "\n=== HTTP Status Codes ===" awk '{print $9}' "$log_file" | sort | uniq -c | sort -rn 自动备份脚本 #!/bin/bash # 自动备份脚本 SOURCE_DIR="/path/to/source" BACKUP_DIR="/path/to/backup" DATE=$(date +%Y%m%d_%H%M%S) BACKUP_NAME="backup_$DATE.tar.gz" # 创建备份 echo "Creating backup..." tar -czf "$BACKUP_DIR/$BACKUP_NAME" "$SOURCE_DIR" # 删除30天前的备份 find "$BACKUP_DIR" -name "backup_*.tar.gz" -mtime +30 -delete echo "Backup completed: $BACKUP_NAME" 最佳实践 代码风格 使用Shebang:始终在脚本开头指定解释器 添加注释:解释复杂逻辑和重要步骤 使用有意义的变量名:避免单字母变量(除循环变量外) 缩进代码:使用一致的缩进(通常是4个空格) 引用变量:始终使用引号包裹变量("$var"而非$var) 安全建议 验证输入:始终验证用户输入和参数 使用绝对路径:避免路径混淆 最小权限原则:只授予必要的权限 清理临时文件:脚本结束时清理 避免eval:除非绝对必要,否则不使用eval 性能优化 避免外部命令:尽量使用内置功能 减少子shell:避免不必要的进程创建 使用管道:而不是临时文件 批量处理:一次处理多个项目 缓存结果:避免重复计算 小结 Shell脚本是系统管理和自动化的强大工具。通过本文,你学习了: ...

November 2, 2015 · 8 min · 1691 words · s-ai-unix
开发工具与编程

开发工具与编程技巧集锦

优秀的开发者不仅要掌握语言本身,更要熟悉各种开发工具和编程技巧。本文汇集了多种语言的实用技巧和工具,帮助你提升开发效率和代码质量。 JavaScript实用技巧 数组操作 基本排序 // 数字数组排序(从小到大) function compare(num1, num2) { return num1 - num2; } var nums = [3, 1, 2, 100, 4, 200]; nums.sort(compare); console.log(nums); // [1, 2, 3, 4, 100, 200] // 从大到小排序 function compareDesc(num1, num2) { return num2 - num1; } nums.sort(compareDesc); console.log(nums); // [200, 100, 4, 3, 2, 1] 注意:JavaScript的sort()方法默认将元素转换为字符串排序,所以对数字需要自定义比较函数。 迭代器方法 // map:创建新数组 function first(word) { return word[0]; } var words = ["for", "your", "info"]; var acronym = words.map(first); console.log(acronym.join("")); // "fyi" // 数值计算 var numbers = [1, 2, 3, 4, 5]; var doubled = numbers.map(x => x * 2); console.log(doubled); // [2, 4, 6, 8, 10] filter过滤 // 筛选及格成绩 function passing(num) { return num >= 60; } var grades = []; for (var i = 0; i < 20; i++) { grades[i] = Math.floor(Math.random() * 101); } var passGrades = grades.filter(passing); console.log("全部成绩:", grades); console.log("及格成绩:", passGrades); // 筛选偶数 var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var evens = nums.filter(n => n % 2 === 0); console.log(evens); // [2, 4, 6, 8, 10] reduce累加 // 数组求和 var numbers = [1, 2, 3, 4, 5]; var sum = numbers.reduce((total, num) => total + num, 0); console.log(sum); // 15 // 数组最大值 var max = numbers.reduce((a, b) => Math.max(a, b)); console.log(max); // 5 // 统计字符出现次数 var str = "hello world"; var charCount = str.split('').reduce((count, char) => { count[char] = (count[char] || 0) + 1; return count; }, {}); console.log(charCount); // {h: 1, e: 1, l: 3, o: 2, ' ': 1, w: 1, r: 1, d: 1} some和every // some:是否存在满足条件的元素 var numbers = [1, 2, 3, 4, 5]; var hasEven = numbers.some(n => n % 2 === 0); console.log(hasEven); // true // every:是否所有元素都满足条件 var allPositive = numbers.every(n => n > 0); console.log(allPositive); // true var allGreaterThanThree = numbers.every(n => n > 3); console.log(allGreaterThanThree); // false forEach遍历 var colors = ["red", "green", "blue"]; colors.forEach((color, index) => { console.log(`${index}: ${color}`); }); // 输出: // 0: red // 1: green // 2: blue 二维数组操作 计算学生平均分 // 计算每个学生的平均分 var grades = [ [89, 77, 78], [76, 82, 81], [91, 94, 89] ]; var total = 0; var average = 0.0; for (var row = 0; row < grades.length; row++) { for (var col = 0; col < grades[row].length; col++) { total += grades[row][col]; } average = total / grades[row].length; console.log("Student " + (row + 1) + " average: " + average.toFixed(2)); total = 0; average = 0.0; } // 输出: // Student 1 average: 81.33 // Student 2 average: 79.67 // Student 3 average: 91.33 计算科目平均分 // 计算每门考试的平均分 var grades = [ [89, 77, 78], [76, 82, 81], [91, 94, 89] ]; var total = 0; var average = 0.0; for (var col = 0; col < grades[0].length; col++) { for (var row = 0; row < grades.length; row++) { total += grades[row][col]; } average = total / grades.length; console.log("Test " + (col + 1) + " average: " + average.toFixed(2)); total = 0; average = 0.0; } // 输出: // Test 1 average: 85.33 // Test 2 average: 84.33 // Test 3 average: 82.67 对象操作 按键排序对象 // 按对象的某个键排序 var obj = [ {name: "Alice", age: 25}, {name: "Bob", age: 20}, {name: "Charlie", age: 30} ]; obj.sort((a, b) => a.age - b.age); console.log(obj); // [ // {name: "Bob", age: 20}, // {name: "Alice", age: 25}, // {name: "Charlie", age: 30} // ] 对象解构 // 解构赋值 var person = {name: "Alice", age: 25, city: "New York"}; var {name, age} = person; console.log(name); // "Alice" console.log(age); // 25 // 嵌套解构 var data = { user: { name: "Bob", address: { city: "Boston" } } }; var {user: {address: {city}}} = data; console.log(city); // "Boston" jQuery实用技巧 jQuery是与否 // 检查jQuery是否加载 if (typeof jQuery === 'undefined') { console.log('jQuery not loaded'); } else { console.log('jQuery loaded'); } // 检查元素是否存在 if ($('#myElement').length) { console.log('Element exists'); } jQuery引入方式 <!-- 方式1:从CDN引入 --> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <!-- 方式2:本地文件 --> <script src="/js/jquery-3.6.0.min.js"></script> <!-- 方式3:使用包管理器 --> <!-- npm install jquery --> <script src="./node_modules/jquery/dist/jquery.min.js"></script> <!-- 方式4:RequireJS --> <script> require(['jquery'], function($) { $(document).ready(function() { console.log('jQuery loaded via RequireJS'); }); }); </script> Python数据结构与算法 链表实现 基础链表 class Node: def __init__(self, value): self.value = value self.next = None def __str__(self): return str(self.value) class LinkedList: def __init__(self): self.head = None self.tail = None def addNode(self, value): node = Node(value) if self.head is None: self.head = node self.tail = node else: self.tail.next = node self.tail = node def __str__(self): if self.head is not None: index = self.head nodeStore = [str(index.value)] while index.next is not None: index = index.next nodeStore.append(str(index.value)) return "LinkedList [ " + "->".join(nodeStore) + " ]" return "LinkedList []" def generateLinkedList(numArray): linkedlist = LinkedList() for i in range(len(numArray)): linkedlist.addNode(numArray[i]) return linkedlist # 使用示例 list1 = generateLinkedList([2, 4, 3]) print(list1) # LinkedList [ 2->4->3 ] 链表相加 class ListsSum: def addLists(self, l1, l2): p1 = l1.head p2 = l2.head carry = 0 linkedlist_sum = LinkedList() while (p1 is not None) or (p2 is not None) or (carry != 0): dig_sum = carry if p1 is not None: dig_sum += p1.value p1 = p1.next if p2 is not None: dig_sum += p2.value p2 = p2.next linkedlist_sum.addNode(dig_sum % 10) carry = dig_sum // 10 return linkedlist_sum # 使用示例 solution = ListsSum() list1 = generateLinkedList([2, 4, 3]) # 342 list2 = generateLinkedList([5, 6, 4]) # 465 print(solution.addLists(list1, list2)) # 807: LinkedList [ 7->0->8 ] 栈的实现 class Stack: def __init__(self): self.items = [] def push(self, item): self.items.append(item) def pop(self): if not self.is_empty(): return self.items.pop() def is_empty(self): return len(self.items) == 0 def peek(self): if not self.is_empty(): return self.items[-1] def size(self): return len(self.items) # 使用示例 stack = Stack() stack.push(1) stack.push(2) stack.push(3) print(stack.pop()) # 3 print(stack.peek()) # 2 print(stack.size()) # 2 算法实践技巧 Java算法练习提示 使用Scanner处理输入 Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); String str = scanner.nextLine(); 数组初始化技巧 // 动态数组 ArrayList<Integer> list = new ArrayList<>(); // 固定大小数组 int[] arr = new int[n]; // 二维数组 int[][] matrix = new int[m][n]; 常用工具方法 // 数组排序 Arrays.sort(arr); // 数组转字符串 Arrays.toString(arr); // 填充数组 Arrays.fill(arr, value); Perl技巧集锦 Perl One-Liners 文本处理 # 删除重复行 perl -ne 'print unless $a{$_}++' file.txt # 查找重复行 perl -ne 'print if $a{$_}++' file.txt # 添加行号 perl -ne 'print "$. $_"' file.txt # 反转行顺序 perl -e 'print reverse <>' file.txt # 随机排序行 perl -e 'print shuffle <>' file.txt 数值计算 # 计算列的总和 perl -lane '$sum += $F[0]; END { print $sum }' file.txt # 计算平均值 perl -lane '$sum += $F[0]; $count++; END { print $sum/$count }' file.txt # 查找最大值 perl -lane '$max = $F[0] if !defined $max || $F[0] > $max; END { print $max }' file.txt Perl高级特性 静态变量 # 使用state定义静态变量(Perl 5.10+) use feature 'state'; sub counter { state $count = 0; return ++$count; } print counter(); # 1 print counter(); # 2 print counter(); # 3 # 老版本方法 sub counter_old { my $count; $count ||= 0; return ++$count; } 匿名子例程 # 创建闭包 sub create_counter { my $count = 0; return sub { return ++$count; }; } my $counter1 = create_counter(); my $counter2 = create_counter(); print $counter1->(); # 1 print $counter1->(); # 2 print $counter2->(); # 1 数据结构实现对比 链表的多种实现 Perl链表实现 package LinkedList; sub new { my $class = shift; my $self = { head => undef, tail => undef, }; bless $self, $class; return $self; } sub add_node { my ($self, $value) = @_; my $node = {value => $value, next => undef}; if (!defined $self->{head}) { $self->{head} = $node; $self->{tail} = $node; } else { $self->{tail}{next} = $node; $self->{tail} = $node; } } C链表实现 typedef struct Node { int value; struct Node* next; } Node; typedef struct LinkedList { Node* head; Node* tail; } LinkedList; void addNode(LinkedList* list, int value) { Node* node = (Node*)malloc(sizeof(Node)); node->value = value; node->next = NULL; if (list->head == NULL) { list->head = node; list->tail = node; } else { list->tail->next = node; list->tail = node; } } 线性表的顺序存储 指针实现(动态数组) typedef struct { int* data; int length; int capacity; } SeqList; void initList(SeqList* list, int capacity) { list->data = (int*)malloc(sizeof(int) * capacity); list->length = 0; list->capacity = capacity; } void insert(SeqList* list, int index, int value) { if (index < 0 || index > list->length) { return; // 索引越界 } if (list->length >= list->capacity) { // 扩容 int newCapacity = list->capacity * 2; int* newData = (int*)realloc(list->data, sizeof(int) * newCapacity); if (newData) { list->data = newData; list->capacity = newCapacity; } } // 移动元素 for (int i = list->length; i > index; i--) { list->data[i] = list->data[i - 1]; } list->data[index] = value; list->length++; } 引用实现(智能指针) #include <memory> #include <vector> class SmartList { private: std::shared_ptr<std::vector<int>> data; public: SmartList() : data(std::make_shared<std::vector<int>>()) {} void insert(int index, int value) { if (index >= 0 && index <= data->size()) { data->insert(data->begin() + index, value); } } int get(int index) const { if (index >= 0 && index < data->size()) { return (*data)[index]; } return -1; // 或抛出异常 } int size() const { return data->size(); } }; 二叉树遍历 递归实现 class TreeNode: def __init__(self, value): self.value = value self.left = None self.right = None def preorder_traversal(node): """前序遍历:根-左-右""" if node: print(node.value) preorder_traversal(node.left) preorder_traversal(node.right) def inorder_traversal(node): """中序遍历:左-根-右""" if node: inorder_traversal(node.left) print(node.value) inorder_traversal(node.right) def postorder_traversal(node): """后序遍历:左-右-根""" if node: postorder_traversal(node.left) postorder_traversal(node.right) print(node.value) 非递归实现(使用栈) def preorder_iterative(root): """前序遍历非递归实现""" if not root: return stack = [root] while stack: node = stack.pop() print(node.value) # 先右后左,保证左子树先处理 if node.right: stack.append(node.right) if node.left: stack.append(node.left) def inorder_iterative(root): """中序遍历非递归实现""" stack = [] current = root while current or stack: # 到达最左节点 while current: stack.append(current) current = current.left current = stack.pop() print(current.value) current = current.right 实用编程技巧 文件批量操作 Perl批量重命名 use strict; use warnings; use Cwd; my $target_dir = getcwd(); opendir(my $dh, $target_dir) || die "can't opendir $target_dir: $!"; my @files = grep { /\w/ && -f "$_" && !/^\./ } readdir($dh); for (@files) { my $file = $_; # 示例:[Alex_Holmes]_Hadoop_in_Practice(BookZZ.org).pdf # 转换为:Hadoop_in_Practice.pdf if (/^(?:\[[\S\s]+\])([\S\s]+)(?:\([\S\s]+\))\.pdf$/) { my $new_name = $1 . ".pdf"; rename($file, $new_name) || die("error in renaming: $!"); } } Python批量操作 import os import re def batch_rename(directory): pattern = re.compile(r'\[.*?\](.*?)\(.*?\)\.pdf$') for filename in os.listdir(directory): match = pattern.match(filename) if match: new_name = match.group(1) + '.pdf' old_path = os.path.join(directory, filename) new_path = os.path.join(directory, new_name) os.rename(old_path, new_path) print(f"Renamed: {filename} -> {new_name}") batch_rename('.') 文本处理技巧 删除^M字符 # Vim中删除DOS换行符 :%s/^M//g # ^M输入方法:Ctrl+V,然后Enter # Perl脚本删除^M并删除注释 open($IN, $ARGV[0]) or die "in: $@"; open($OUT, ">", $ARGV[0] . ".new") or die "out: $@"; while (<$IN>) { my $line = $_; $line =~ s/(\/\/.*)//g; # 删除C风格注释 $line =~ s/\r//g; # 删除^M print $OUT $line; } close($IN); close($OUT); # 转换并替换原文件 $command = "mv $ARGV[0].new $ARGV[0] && chmod 777 $ARGV[0] && dos2unix $ARGV[0]"; system($command); 命令行工具技巧 按行长度排序 # 按行长度从长到短排序 cat file.txt | awk '{ print length($0) " " $0; }' | sort -r -n | cut -d ' ' -f 2- # 按行长度从短到长排序 cat file.txt | awk '{ print length($0) " " $0; }' | sort -n | cut -d ' ' -f 2- 提取公共行 # 查找多个文件中的公共行 grep -F -x -f file1 file2 file3 # 查找在file1中但不在file2中的行 grep -F -x -v -f file2 file1 统计最常用命令 # 查看最常用的10个命令 history | awk '{a[$2]++} END {for(i in a) {print a[i]" "i}}' | sort -rn | head 模块化编程 创建可重用模块 # MyUtils.pm package MyUtils; use strict; use warnings; use Exporter 'import'; our @EXPORT_OK = qw(add multiply); sub add { my ($a, $b) = @_; return $a + $b; } sub multiply { my ($a, $b) = @_; return $a * $b; } 1; # 使用模块 use MyUtils qw(add multiply); print add(2, 3); # 5 print multiply(2, 3); # 6 Python模块化 # utils.py def add(a, b): return a + b def multiply(a, b): return a * b # main.py from utils import add, multiply print(add(2, 3)) # 5 print(multiply(2, 3)) # 6 性能优化技巧 尾递归优化 // 普通递归阶乘 int factorial(int n) { if (n <= 1) return 1; return n * factorial(n - 1); } // 尾递归优化版本 int factorial_tail(int n, int accumulator) { if (n <= 1) return accumulator; return factorial_tail(n - 1, n * accumulator); } int factorial(int n) { return factorial_tail(n, 1); } 记忆化技术 # Fibonacci记忆化 from functools import lru_cache @lru_cache(maxsize=None) def fibonacci(n): if n < 2: return n return fibonacci(n - 1) + fibonacci(n - 2) # 手动实现记忆化 def fibonacci_memo(): cache = {} def fib(n): if n in cache: return cache[n] if n < 2: result = n else: result = fib(n - 1) + fib(n - 2) cache[n] = result return result return fib fib = fibonacci_memo() 小结 本文汇集了多种编程语言和工具的实用技巧: ...

August 31, 2014 · 10 min · 2011 words · s-ai-unix
服务器机房

系统管理与环境配置实战指南

搭建和管理Web服务器环境是开发者的必备技能。本文将介绍从基础环境搭建到高级配置的完整流程,帮助你在本地和生产环境中快速部署可靠的服务。 Apache服务器配置 macOS上配置Apache macOS系统自带Apache,只需简单配置即可使用。 启用PHP支持 # 1. 编辑Apache配置文件 sudo vim /etc/apache2/httpd.conf # 2. 启用PHP模块 # 取消这行的注释: # LoadModule php5_module libexec/apache2/libphp5.so # 3. 保存并退出 # 4. 复制PHP配置文件 sudo cp /etc/php.ini.default /etc/php.ini # 5. 启动Apache sudo apachectl start # 6. 创建软链接方便访问 ln -s /Library/WebServer/Documents ~/www # 7. 重命名或删除默认的index.html.en Apache控制命令 # 启动Apache sudo apachectl start # 停止Apache sudo apachectl stop # 重启Apache sudo apachectl restart # 查看Apache状态 sudo apachectl status # 测试配置文件语法 sudo apachectl configtest # 重新加载配置(不中断服务) sudo apachectl graceful 配置虚拟主机 在CentOS或其他Linux系统上,可以在主配置文件中包含自定义配置。 ...

June 7, 2014 · 5 min · 1045 words · s-ai-unix