给一个模块分配一个编号

huangapple go评论56阅读模式
英文:

Assiging a number to a module

问题

我需要能够为一个模块分配一个数字,我的当前代码如下。空白部分是需要分配的地方。

```bash
assignmodules(){
#分配第一个模块
x=1
modules=$(ls ./modules)
for module in modules; do
   
  echo "$module 分配到槽位 $x"
  x=$(x+1)
done
}

一些事情:
模块标题必须在不同的函数中打印,所以我不能在for循环内打印它。
模块需要以以下方式运行:

case $choice in 
1) module1
2) module2
3) module3
等等

需要按以下方式打印(如果我找到一个好的解决方案,即使做得不对,我可能仍然会使用它):

[1] 模块1 [2] 模块2
[3] 模块3 [4] 模块4
等等

我在空白处尝试了这个:
[{x}]module=$module
(我不认为这正是我尝试的内容,我相信它有些不同,但我记不清了)
我希望它能按上面描述的方式运行,但我不认为会实现。


<details>
<summary>英文:</summary>

I need to be able to assign a number to a module, my current code is below. Gap is where it needs to be assigned.

assignmodules(){
#Assign first module
x=1
modules=$(ls ./modules)
for module in modules; do

echo "$module assigned to slot $x"
x=$(x+1)
done
}

A few things:
Module title has to be printed in a different function, so i cant print it within the for loop.
Module needs to be run this way:

case $choice in

  1. module1
  2. module2
  3. module3
    etc.
Needs to be printed as following (If i find a good a solution that doesn&#39;t do it right, ill probably still use that.):

[1] Module1 [2] Module2
[3] Module3 [4] Module4
etc.

I tried this in the blank:
[{x}]module=$module
(i don&#39;t think this is exactly what i tried, i believe it was slightly different but i cant remember)
I wanted it to do as described above, but i don&#39;t think it will.

</details>


# 答案1
**得分**: 2

```plaintext
如果您正在使用Bash,只需使用数组和一些通配符和特殊参数扩展。

assignmodules() {
    set -- ./modules/*
    modules=(0 "${@/#./modules/}")
    unset 'modules[0]'
    declare -p modules # 可选择性地显示结果
}

在某个地方,您可以使用以下代码打印模块列表:

for i in "${!modules[@]}"; do
    echo "[$i] ${modules[i]}"
done

或者

for i in "${!modules[@]}"; do
    echo -n "[$i] ${modules[i]} "
    (( i % 2 == 0 )) && echo
done

(( i % 2 )) && echo

建议在脚本开头添加 `shopt -s nullglob`,特别是如果模块目录有时可能是空的。

最后,如果您想要询问用户选择,可以查看 `select` 命令。您可能不需要手动显示它们。运行 `help select`。
英文:

If you're using Bash, just use an array and some globbing and special parameter expansion.

assignmodules() {
	set -- ./modules/*
	modules=(0 &quot;${@/#./modules/}&quot;)
	unset &#39;modules[0]&#39;
	declare -p modules # Optionally show result
}

Somewhere you can print the modules list using

for i in &quot;${!modules[@]}&quot;; do
    echo &quot;[$i] ${modules[i]}&quot;
done

Or

for i in &quot;${!modules[@]}&quot;; do
    echo -n &quot;[$i] ${modules[i]} &quot;
    (( i % 2 == 0 )) &amp;&amp; echo
done

(( i % 2 )) &amp;&amp; echo

It's also recommended to add shopt -s nullglob at the beginning of the script, especially if
modules directory can sometimes be empty.

Lastly, if you're wanting to ask user for choices, look at the select command. You may not need to display them manually. Run help select.

答案2

得分: 1

只需将您的模块放入一个数组中:数值索引数组(默认类型)的索引自然是按编号排序的(请注意这些数字从0开始,而不是1)。

assignmodules() {
  declare -ga modules            # 明确声明全局数组(可选)
  modules=( modules/* )          # 将所有模块目录条目放入一个数组中
  modules=( "${modules[@]#*/}" ) # 剥离每个条目的模块/前缀
}

assignmodules                    # 调用该函数会将模块分配

# 要以特定要求的格式打印模块列表可能如下所示:
print_args=( )                            # 生成格式字符串参数数组
for module_idx in "${!modules[@]}"; do   # 遍历数组中的索引
  module=${modules[$module_idx]}          # 检索相应的模块名称
  print_args+=( "[$module_idx]" "$module" ) # 追加到要打印的内容列表中
done

# 格式字符串重复直到所有参数被使用
# 因此,这样可以按照您的请求得到两列
printf '%s %s\t%s %s\n' "${print_args[@]}" # 实际打印我们的列表
英文:

Just put your modules in an array: Numerically-indexed arrays (the default kind) have indices that are numbered by nature (note that these numbers start at 0, not 1).

assignmodules() {
  declare -ga modules            # explicitly declare global array (optional)
  modules=( modules/* )          # put all modules directory entries in an array
  modules=( &quot;${modules[@]#*/}&quot; ) # strip modules/ prefix off each entry
}

assignmodules                    # calling that function leaves modules assigned

# to print your list of modules in the specific format requested might look like:
print_args=( )                            # generate format string argument array
for modules_idx in &quot;${!modules[@]}&quot;; do   # iterate over indices in array
  module=${modules[$module_idx]}          # retrieve corresponding module name
  print_args+=( &quot;[$module_idx]&quot; &quot;$module&quot; ) # append to list of stuff to print
done

# format string is repeated until all arguments are consumed
# so this way we get two columns as you requested
printf &#39;%s %s\t%s %s\n&#39; &quot;${print_args[@]}&quot; # actually print our list

huangapple
  • 本文由 发表于 2023年2月7日 04:26:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定