首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以将10个类似的函数转换为for循环?

是否可以将10个类似的函数转换为for循环?
EN

Stack Overflow用户
提问于 2016-04-03 21:10:03
回答 3查看 98关注 0票数 1

我正在尝试将信息从我的工作区输送到另一个命令中,因为每个工作区包含相同的信息,只有一个不同的数字来标识它,所以我编写了10个函数,其中唯一的区别是一些变量名称中的一个字符。我觉得这可以大大简化,但我想不出在我的情况下如何让任何循环工作。

这里都是我的脚本,包含以下10个函数:

代码语言:javascript
复制
#!/bin/bash

# Include config file.
. $(dirname $0)/config

getWorkspaceInfo(){
    filledWorkspaces=$(i3-msg -t get_workspaces | grep -Po '"'"name"'"\s*:\s*"\K([^"]*)')
    currentWorkspace=$(i3-msg -t get_outputs | sed 's/.*"current_workspace":"\([^"]*\)".*/\1/')
}

# Determine the status of each workspace. Current is green, unfocused is white, empty is grey.
workspace1(){
    if [[ ${currentWorkspace} -eq 1 ]]; then
        workspace1Color=${green}
    elif [[ $(echo ${filledWorkspaces} | grep -w "1") == "" ]]; then
        workspace1Color=${grey}
    else
        workspace1Color=${foreground}
    fi

    echo "%{F${workspace1Color}}${workspace1Name}"
}

workspace2(){
    if [[ ${currentWorkspace} -eq 2 ]]; then
        workspace2Color=${green}
    elif [[ $(echo ${filledWorkspaces} | grep -w "2") == "" ]]; then
        workspace2Color=${grey}
    else
        workspace2Color=${foreground}
    fi

    echo "%{F${workspace2Color}}${workspace2Name}"
}

workspace3(){
    if [[ ${currentWorkspace} -eq 3 ]]; then
        workspace3Color=${green}
    elif [[ $(echo ${filledWorkspaces} | grep -w "3") == "" ]]; then
        workspace3Color=${grey}
    else
        workspace3Color=${foreground}
    fi

    echo "%{F${workspace3Color}}${workspace3Name}"
}

workspace4(){
    if [[ ${currentWorkspace} -eq 4 ]]; then
        workspace4Color=${green}
    elif [[ $(echo ${filledWorkspaces} | grep -w "4") == "" ]]; then
        workspace4Color=${grey}
    else
        workspace4Color=${foreground}
    fi

    echo "%{F${workspace4Color}}${workspace4Name}"
}

workspace5(){
    if [[ ${currentWorkspace} -eq 5 ]]; then
        workspace5Color=${green}
    elif [[ $(echo ${filledWorkspaces} | grep -w "5") == "" ]]; then
        workspace5Color=${grey}
    else
        workspace5Color=${foreground}
    fi

    echo "%{F${workspace5Color}}${workspace5Name}"
}

workspace6(){
    if [[ ${currentWorkspace} -eq 6 ]]; then
        workspace6Color=${green}
    elif [[ $(echo ${filledWorkspaces} | grep -w "6") == "" ]]; then
        workspace6Color=${grey}
    else
        workspace6Color=${foreground}
    fi

    echo "%{F${workspace6Color}}${workspace6Name}"
}

workspace7(){
    if [[ ${currentWorkspace} -eq 7 ]]; then
        workspace7Color=${green}
    elif [[ $(echo ${filledWorkspaces} | grep -w "7") == "" ]]; then
        workspace7Color=${grey}
    else
        workspace7Color=${foreground}
    fi

    echo "%{F${workspace7Color}}${workspace7Name}"
}

workspace8(){
    if [[ ${currentWorkspace} -eq 8 ]]; then
        workspace8Color=${green}
    elif [[ $(echo ${filledWorkspaces} | grep -w "8") == "" ]]; then
        workspace8Color=${grey}
    else
        workspace8Color=${foreground}
    fi

    echo "%{F${workspace8Color}}${workspace8Name}"
}

workspace9(){
    if [[ ${currentWorkspace} -eq 9 ]]; then
        workspace9Color=${green}
    elif [[ $(echo ${filledWorkspaces} | grep -w "9") == "" ]]; then
        workspace9Color=${grey}
    else
        workspace9Color=${foreground}
    fi

    echo "%{F${workspace9Color}}${workspace9Name}"
}

workspace10(){
    if [[ ${currentWorkspace} -eq 10 ]]; then
        workspace10Color=${green}
    elif [[ $(echo ${filledWorkspaces} | grep -w "10") == "" ]]; then
        workspace10Color=${grey}
    else
        workspace10Color=${foreground}
    fi

    echo "%{F${workspace10Color}}${workspace10Name}"
}

# Pipe functions to the bar infinitely.
while true; do
    getWorkspaceInfo
    echo "%{c}$(workspace1)${separator}$(workspace2)${separator}$(workspace3)${separator}$(workspace4)${separator}$(workspace5)${separator}$(workspace6)${separator}$(workspace7)${separator}$(workspace8)${separator}$(workspace9)${separator}$(workspace10)"
done | lemonbar -g ${panelWidth}x${panelHeight}+${panelX}+${bottomPanelY} -f "${font}" -f "${iconFont}" -B "${background}" -F "${foreground}" -p -d  | \
    while true; do read line; eval $line; done &

下面是我要导入的配置文件:

代码语言:javascript
复制
#!/bin/bash

# Outside sources
xres="$HOME/.Xresources"
i3config="$HOME/.config/i3/config"

# Fetch information from Xresources
background=$(cat ${xres} | grep -i background | tail -c 8)
foreground=$(cat ${xres} | grep -i foreground | tail -c 8)
black=$(cat ${xres} | grep -i color0 | tail -c 8)
grey=$(cat ${xres} | grep -i color8 | tail -c 8)
red=$(cat ${xres} | grep -i color9 | tail -c 8)
green=$(cat ${xres} | grep -i color10 | tail -c 8)
yellow=$(cat ${xres} | grep -i color11 | tail -c 8)
blue=$(cat ${xres} | grep -i color12 | tail -c 8)
magenta=$(cat ${xres} | grep -i color13 | tail -c 8)
cyan=$(cat ${xres} | grep -i color14 | tail -c 8)
white=$(cat ${xres} | grep -i color15 | tail -c 8)

# Fetch information from i3 config
gapSize=$(cat ${i3config} | grep -i "gaps inner" | awk '{print $3}')

# Workspace names -- independant from i3 config -- workspaces in i3 config should be named numbers 1-10.
workspace1Name="Web Browser"
workspace2Name="Terminal"
workspace3Name="Text Editor"
workspace4Name="Unspecified"
workspace5Name="Unspecified"
workspace6Name="Unspecified"
workspace7Name="Unspecified"
workspace8Name="Unspecified"
workspace9Name="Messenger"
workspace10Name="Music Player"

# Fonts
font="InputSans-10"
iconFont="FontAwesome"

separator="%{F$foreground}   |│|   "

# Panel size
screenWidth=$(xrandr | grep 'Screen 0'| awk '{print $8}')
screenHeight=$(xrandr | grep 'Screen 0' | awk '{print $10}' | tr -d ",")

panelHeight=$((${gapSize} * 2))
panelWidth=$((${screenWidth} - ${panelHeight}))
panelX=${gapSize}
topPanelY=${gapSize}
bottomPanelY=$((${screenHeight} - ${panelHeight} - ${gapSize}))
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-03 21:38:46

最简单的解决办法是编写如下内容:

代码语言:javascript
复制
function all_10_workspaces () {
    local i
    for i in {1..10} ; do
        local workspaceNameVar="workspace${i}Name"
        local workspaceName="${!workspaceNameVar}"

        local color
        if (( currentWorkspace == 1 )) ; then
            color=$green
        elif grep -w -q "$i" <<< "$filledWorkspaces" ; then
            color=$foreground
        else
            color=$grey
        fi

        echo "%{F$color}$workspaceName"
    done
}

。。。但是,您确实应该考虑使用数组。例如:

代码语言:javascript
复制
workspaceNames=(
    ''             # 0 (no such workspace)
    'Web Browser'  # 1
    Terminal       # 2
    'Text Editor'  # 3
    Unspecified    # 4
    Unspecified    # 5
    Unspecified    # 6
    Unspecified    # 7
    Unspecified    # 8
    Messenger      # 9
    'Music Player' # 10
)

然后,例如,工作区#7被命名为"${workspaceNames[7]}",并给出一个变量i,工作区#i被命名为"${workspaceNames[i]}"

票数 4
EN

Stack Overflow用户

发布于 2016-04-03 23:20:09

可能是这样吧?

代码语言:javascript
复制
workspaceCount=10

while true; do
  # Output will look like "%{c}$(workspace1Color)${separator}$(workspace2Color)${separator}...."

  # This is what is sent before the first item in each line
  itemSep="%{c}"

  for i in {1..$workspaceCount}; do

    if [ ${currentWorkspace} -eq $i ]; then
      color="${green}"
    elif [[ $(echo ${filledWorkspaces} | grep -w "1") == "" ]]; then
      color="${grey}"
    else
      color="${foreground}"
    fi

    echo -n "${itemSep}${color}"
    itemSep="${separator}"

  done
  echo   # Send LF after all items

done
票数 1
EN

Stack Overflow用户

发布于 2016-04-04 00:43:40

我想出了一种方法来获得我想要的东西,使用鲁阿克和菲尔·弗里德的想法,以及我自己想出来的东西。这可能不是解决问题的最短或最有效的方法,但它比有10个单独的函数要短得多。

代码语言:javascript
复制
#!/bin/bash

# Include config file.
. $(dirname $0)/config

getWorkspaceInfo(){
    filledWorkspaces=$(i3-msg -t get_workspaces | grep -Po '"'"name"'"\s*:\s*"\K([^"]*)')
    currentWorkspace=$(i3-msg -t get_outputs | sed 's/.*"current_workspace":"\([^"]*\)".*/\1/')
}

# Determine the status of each workspace. Current is green, unfocused is white, empty is grey.
workspaces(){
    workspaces=""
    currentSeparator="${separator}"

    for i in {1..10} ; do
        if [[ ${currentWorkspace} -eq ${i} ]]; then
            color=${green}
        elif [[ $(echo ${filledWorkspaces} | grep -w "${i}") == "" ]]; then
            color=${grey}
        else
            color=${foreground}
        fi

        if [[ ${i} -eq 10 ]]; then
            currentSeparator=""
        fi

        workspaces+="%{F$color}${workspaceNames[i]}${currentSeparator}"
    done

    echo "${workspaces}"
}

# Pipe functions to the bar infinitely.
while true; do
    getWorkspaceInfo
    echo "%{c}$(workspaces)"
done | lemonbar -g ${panelWidth}x${panelHeight}+${panelX}+${bottomPanelY} -f "${font}" -f "${iconFont}" -B "${background}" -F "${foreground}" -p -d | \
    while true; do read line; eval $line; done &

为了尽可能简单地解释它所做的事情:循环遍历所有10个工作区,将一个函数的输出添加到一个新变量的末尾。由于不能在每个函数调用之间添加分隔符,所以我只是将分隔符添加到回显的末尾,确保没有使用for循环将分隔符添加到最后一个工作区,该循环将分隔符变量设置为null。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36391226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档