What's Macro Script?

 
#!/bin/bash # Config sync script for synchronizing config macros with js_list files # Usage: ./config_sync.sh <model_name> # Example: ./config_sync.sh MR100V3 MODEL_NAME="$1" if [ -z "$MODEL_NAME" ]; then echo "Usage: $0 <model_name>" echo "Example: $0 MR100v3" exit 1 fi CONFIG_PATH="config" # Fixed config directory CONFIG_FILE="${CONFIG_PATH}/${MODEL_NAME}.config" HELP_LIST_FILE="${CONFIG_PATH}/${MODEL_NAME}/multipleLanguage/help.js_list" STR_LIST_FILE="${CONFIG_PATH}/${MODEL_NAME}/multipleLanguage/str.js_list" CONFIG_MAP_FILE="config_map.txt" if [ ! -f "$CONFIG_FILE" ]; then echo "Error: Config file $CONFIG_FILE not found!" exit 1 fi if [ ! -f "$HELP_LIST_FILE" ]; then echo "Error: Help list file $HELP_LIST_FILE not found!" exit 1 fi if [ ! -f "$STR_LIST_FILE" ]; then echo "Error: String list file $STR_LIST_FILE not found!" exit 1 fi if [ ! -f "$CONFIG_MAP_FILE" ]; then echo "Error: Config map file $CONFIG_MAP_FILE not found!" exit 1 fi echo "Synchronizing config for model: $MODEL_NAME" echo "Config file: $CONFIG_FILE" echo "Help list: $HELP_LIST_FILE" echo "String list: $STR_LIST_FILE" echo "Config map: $CONFIG_MAP_FILE" echo "----------------------------------------" # Function to check if a macro is enabled in config # Handles all formats: # - INCLUDE_XXX=y (enabled) # - # INCLUDE_XXX=y (disabled) # - # INCLUDE_XXX is not set (disabled) # - #INCLUDE_XXX is not set (disabled) is_macro_enabled() { local macro="$1" if grep -q "^${macro}=y" "$CONFIG_FILE"; then return 0 # enabled elif grep -q "^#[[:space:]]*${macro}=y\|^#.*${macro}[[:space:]]*is[[:space:]]*not[[:space:]]*set" "$CONFIG_FILE"; then return 1 # disabled (commented out) else return 2 # not found fi } # Function to process a single token in a specific file # Parameters: token, file_path, macro_enabled process_single_token() { local token="$1" local file="$2" local macro_enabled="$3" # Skip if token is empty if [ -z "$token" ]; then return fi # Determine file type for logging based on file path local file_type if [[ "$file" == *"str.js_list"* ]]; then file_type="str" else file_type="help" fi # Find lines containing this token (exact match) local found_lines=$(grep -n "[[:space:]]*${token}[[:space:]]*$" "$file" 2>/dev/null || true) if [ -z "$found_lines" ]; then # Try exact match as fallback found_lines=$(grep -n "^#*${token}$" "$file" 2>/dev/null || true) fi if [ -z "$found_lines" ]; then echo "Warning: Token '$token' not found in $file_type file" return fi # Process each matching line (usually just one) while IFS= read -r line_info; do local line_num=$(echo "$line_info" | cut -d: -f1) local line_content=$(echo "$line_info" | cut -d: -f2-) # Check current status if [[ "$line_content" =~ ^[[:space:]]*#.* ]]; then local item_enabled=false else local item_enabled=true fi # Sync status if needed if [ "$macro_enabled" != "$item_enabled" ]; then if [ "$macro_enabled" = "true" ]; then # Enable: remove the # comment sed -i "${line_num}s/^[[:space:]]*#[[:space:]]*//" "$file" echo "Enabled: $token in $file_type ($line_num)" else # Disable: add # comment sed -i "${line_num}s/^[[:space:]]*/#/" "$file" echo "Disabled: $token in $file_type ($line_num)" fi changes_made=true else echo "Already synchronized: $token in $file_type" fi done <<< "$found_lines" } # Function to find and check/set status of items in js_list files # Processes fixed pair of tokens: first for str.js_list, second for help.js_list process_token_mapping() { local macro="$1" local macro_enabled="$2" local string_token="$3" local help_token="$4" # Process string token (first token - always goes to str.js_list) process_single_token "$string_token" "$STR_LIST_FILE" "$macro_enabled" # Process help token (second token - always goes to help.js_list) process_single_token "$help_token" "$HELP_LIST_FILE" "$macro_enabled" } echo "Starting synchronization..." changes_made=false # Process config map file line by line while IFS= read -r line; do # Skip empty lines and comments [[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue # Parse line: MACRO string_token help_token read -ra parts <<< "$line" if [ ${#parts[@]} -ne 3 ]; then echo "Warning: Invalid mapping line format. Expected: MACRO string_token help_token. Got: $line" continue fi macro="${parts[0]}" string_token="${parts[1]}" help_token="${parts[2]}" # Check if macro exists in config file is_macro_enabled "$macro" macro_check_result=$? if [ $macro_check_result -eq 2 ]; then echo "Warning: Macro '$macro' not found in config file, skipping..." continue elif [ $macro_check_result -eq 0 ]; then macro_enabled=true else macro_enabled=false fi echo "Processing: $macro (enabled: $macro_enabled) -> str: $string_token, help: $help_token" # Process the pair of tokens for this macro process_token_mapping "$macro" "$macro_enabled" "$string_token" "$help_token" done < "$CONFIG_MAP_FILE" echo "----------------------------------------" if [ "$changes_made" = true ]; then echo " Synchronization completed with changes made" else echo " Synchronization completed - all files already in sync" fi exit 0 这个脚本如果要用在企业中,还有哪些地方可以更改的更清晰
11-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值