set -- $line 怎么理解

本文介绍了如何在bash脚本中使用IFS(Internal Field Separator)重设位置参数,包括使用IFS进行参数设置和输出特定参数的方法。
 man bash: you will find below
--      If  no  arguments  follow this option, then the positional parameters are unset.  Otherwise, the positional parameters are set to
                      the args, even if some of them begin with a -.
for example:
  1. june@~ 01:42:16>
  2. line=sadasd:ddddd; IFS=: ; set -- $line;  echo $1; echo $2
  3. sadasd
  4. ddddd
  5. june@~ 01:42:26>
根据IFS重设位置参数
URL_LIBDIR=${URL_LIBFIR:-/lib/urlfilter} position=0 ids="" action="" usergroupname="" content_filter="" wireless_flag="" wireled_condition="" wireless_support="" ssid="" wireless_iface="" src_type="" profile=`uci -q -c /etc/profile.d get profile.@global[0].support_wireless` [ "$profile" == "1" ] && wireless_support="1" _set_urlfilter_wirelessIface() { local section_name="$1" local tmp_ssid_id="$2" config_get id $section_name id config_get ifname $section_name ifname if [ "$id" -eq "$tmp_ssid_id" ];then if [ -z "$wireless_iface" ];then wireless_iface="$ifname" else wireless_iface="$wireless_iface"",""$ifname" fi fi } get_other_info() { local target_name=$1 lua /lib/urlfilter/find_target_category.lua $target_name local line_number=0 local line="" while read line do line_number=$(($line_number+1)) [ $line_number -eq 1 ] && position=$line [ $line_number -eq 2 ] && ids=$line [ $line_number -eq 3 ] && action=$line [ $line_number -eq 4 ] && usergroupname=$line [ $line_number -eq 5 ] && wireless_iface=$line [ $line_number -eq 6 ] && content_filter=$line [ $line_number -eq 7 ] && action1=$line [ $line_number -eq 8 ] && ssid=$line [ $line_number -eq 9 ] && src_type=$line [ $line_number -eq 10 ] && redirect=$line done </tmp/url_category_rule.tmp } { flock 9 type=$1 enable=$(uci get filter_global.@filter_global[0].enable) [ "$enable" != '1' -a "$enable" != 'on' ] && return 0 case $type in add) urlgrp=$2 tmp_act="accept" get_other_info $urlgrp if [ "$wireless_support" == "1" ] && [ "$ssid" != '' ];then if [ "$urlfilter_delay_flag" -eq "1" ];then wireless_iface='' for ssid_tmp in ${ssid};do config_load "ssid_list" config_foreach _set_urlfilter_wirelessIface rule_ssid_info "$ssid_tmp" done uci set url_filter.${urlgrp}.wireless_iface=${wireless_iface} uci commit url_filter fi fi [ "$action" == "drop" ] && tmp_act="drop" if [ "$wireless_support" == "1" ];then if [ -n "$wireless_iface" ] && [ "${wireless_iface:0:2}" == "ra" -o "${wireless_iface:0:3}" == "ath" ];then wireless_flag="1" fi fi if [ $position == "0" ];then position="-A url_filter" else position="-I url_filter $position" fi if [ "$wireless_flag" == "1" ];then iptables -w -t mangle $position -m conntrack --ctdir ORIGINAL -p tcp\ -m bridgeport --multisports $wireless_iface -m comment --comment url_${urlgrp}_url\ -j urlfilter_target --type $tmp_act --category $ids elif [ "$src_type" == "network" ]; then iptables -w -t mangle $position -m conntrack --ctdir ORIGINAL -p tcp\ -m comment --comment url_${urlgrp}_url -m set --match-set "@"$usergroupname src\ -j urlfilter_target --type $tmp_act --category $ids else if [ x"$usergroupname" == "xIPGROUP_ANY" ];then iptables -w -t mangle $position -m conntrack --ctdir ORIGINAL -p tcp\ -m comment --comment url_${urlgrp}_url -j urlfilter_target --type $tmp_act --category $ids else iptables -w -t mangle $position -m conntrack --ctdir ORIGINAL -p tcp\ -m comment --comment url_${urlgrp}_url -m set --match-set $usergroupname src\ -j urlfilter_target --type $tmp_act --category $ids fi fi ;; del) key=url_$2_url ret=$(iptables -w -t mangle -n -L url_filter --line-number | grep "\b$key\b" | cut -d ' ' -f 1) [ "$ret" != "" ] && { iptables -w -t mangle -D url_filter $ret } ;; *) return ;; esac
最新发布
10-12
以下是对这段Shell脚本代码的详细讲解: ### 变量初始化 ```bash URL_LIBDIR=${URL_LIBFIR:-/lib/urlfilter} position=0 ids="" action="" usergroupname="" content_filter="" wireless_flag="" wireled_condition="" wireless_support="" ssid="" wireless_iface="" src_type="" profile=`uci -q -c /etc/profile.d get profile.@global[0].support_wireless` [ "$profile" == "1" ] && wireless_support="1" ``` - `URL_LIBDIR`:通过参数扩展的方式设置,如果`URL_LIBFIR`未定义,则使用`/lib/urlfilter`作为默认值。 - 后续的一系列变量用于存储URL过滤规则的相关信息,如位置、ID、动作、用户组名等。 - `profile`:使用`uci`工具从配置文件中获取`profile.@global[0].support_wireless`的值,如果为`1`,则将`wireless_support`设置为`1`,表示支持无线功能。 ### 函数定义 #### `_set_urlfilter_wirelessIface` 函数 ```bash _set_urlfilter_wirelessIface() { local section_name="$1" local tmp_ssid_id="$2" config_get id $section_name id config_get ifname $section_name ifname if [ "$id" -eq "$tmp_ssid_id" ];then if [ -z "$wireless_iface" ];then wireless_iface="$ifname" else wireless_iface="$wireless_iface"",""$ifname" fi fi } ``` - 该函数用于根据`ssid`配置信息,获取对应的无线接口名称。 - 接收两个参数:`section_name`和`tmp_ssid_id`。 - 通过`config_get`函数从配置文件中获取`id`和`ifname`。 - 如果`id`等于`tmp_ssid_id`,则将`ifname`添加到`wireless_iface`中。 #### `get_other_info` 函数 ```bash get_other_info() { local target_name=$1 lua /lib/urlfilter/find_target_category.lua $target_name local line_number=0 local line="" while read line do line_number=$(($line_number+1)) [ $line_number -eq 1 ] && position=$line [ $line_number -eq 2 ] && ids=$line [ $line_number -eq 3 ] && action=$line [ $line_number -eq 4 ] && usergroupname=$line [ $line_number -eq 5 ] && wireless_iface=$line [ $line_number -eq 6 ] && content_filter=$line [ $line_number -eq 7 ] && action1=$line [ $line_number -eq 8 ] && ssid=$line [ $line_number -eq 9 ] && src_type=$line [ $line_number -eq 10 ] && redirect=$line done </tmp/url_category_rule.tmp } ``` - 该函数用于从`/tmp/url_category_rule.tmp`文件中读取URL过滤规则的相关信息。 - 接收一个参数:`target_name`。 - 调用`lua`脚本`/lib/urlfilter/find_target_category.lua`,并将`target_name`作为参数传递给它。 - 通过`while`循环逐行读取`/tmp/url_category_rule.tmp`文件,并根据行号将相应的信息赋值给对应的变量。 ### 主程序逻辑 ```bash { flock 9 type=$1 enable=$(uci get filter_global.@filter_global[0].enable) [ "$enable" != '1' -a "$enable" != 'on' ] && return 0 case $type in add) urlgrp=$2 tmp_act="accept" get_other_info $urlgrp if [ "$wireless_support" == "1" ] && [ "$ssid" != '' ];then if [ "$urlfilter_delay_flag" -eq "1" ];then wireless_iface='' for ssid_tmp in ${ssid};do config_load "ssid_list" config_foreach _set_urlfilter_wirelessIface rule_ssid_info "$ssid_tmp" done uci set url_filter.${urlgrp}.wireless_iface=${wireless_iface} uci commit url_filter fi fi [ "$action" == "drop" ] && tmp_act="drop" if [ "$wireless_support" == "1" ];then if [ -n "$wireless_iface" ] && [ "${wireless_iface:0:2}" == "ra" -o "${wireless_iface:0:3}" == "ath" ];then wireless_flag="1" fi fi if [ $position == "0" ];then position="-A url_filter" else position="-I url_filter $position" fi if [ "$wireless_flag" == "1" ];then iptables -w -t mangle $position -m conntrack --ctdir ORIGINAL -p tcp\ -m bridgeport --multisports $wireless_iface -m comment --comment url_${urlgrp}_url\ -j urlfilter_target --type $tmp_act --category $ids elif [ "$src_type" == "network" ]; then iptables -w -t mangle $position -m conntrack --ctdir ORIGINAL -p tcp\ -m comment --comment url_${urlgrp}_url -m set --match-set "@"$usergroupname src\ -j urlfilter_target --type $tmp_act --category $ids else if [ x"$usergroupname" == "xIPGROUP_ANY" ];then iptables -w -t mangle $position -m conntrack --ctdir ORIGINAL -p tcp\ -m comment --comment url_${urlgrp}_url -j urlfilter_target --type $tmp_act --category $ids else iptables -w -t mangle $position -m conntrack --ctdir ORIGINAL -p tcp\ -m comment --comment url_${urlgrp}_url -m set --match-set $usergroupname src\ -j urlfilter_target --type $tmp_act --category $ids fi fi ;; del) key=url_$2_url ret=$(iptables -w -t mangle -n -L url_filter --line-number | grep "\b$key\b" | cut -d ' ' -f 1) [ "$ret" != "" ] && { iptables -w -t mangle -D url_filter $ret } ;; *) return ;; esac } ``` - 使用`flock`命令对文件描述符`9`进行加锁,确保脚本在同一时间只有一个实例运行。 - 获取脚本的第一个参数`type`,并检查`filter_global.@filter_global[0].enable`的值,如果为`1`或`on`,则继续执行,否则返回。 - 根据`type`的值进行不同的操作: - **add**: - 获取第二个参数`urlgrp`,并调用`get_other_info`函数获取相关信息。 - 如果支持无线功能且`ssid`不为空,并且`urlfilter_delay_flag`为`1`,则根据`ssid`获取对应的无线接口名称,并更新`url_filter`配置文件。 - 根据`action`的值设置`tmp_act`为`accept`或`drop`。 - 如果支持无线功能且`wireless_iface`符合条件,则将`wireless_flag`设置为`1`。 - 根据`position`的值设置`iptables`规则的插入位置。 - 根据不同的条件,使用`iptables`命令添加相应的规则。 - **del**: - 构建规则的注释关键字`key`。 - 使用`iptables`命令查找包含该关键字的规则,并获取其行号。 - 如果行号不为空,则使用`iptables`命令删除该规则。 - **其他**:直接返回。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值