Creating loop iteration using Array and create zip file

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

Creating loop iteration using Array and create zip file

问题

I've translated the code portion you provided:

尝试根据目录创建zip文件,以基于dirs命令声明的当前目录启动进程。然后根据分配给dirs的目录以数组格式存储。使用for循环(sub_dir)从dirs[@]中获取输入数据作为列表,并在创建zip文件之前打印文件名。

运行脚本时我没有看到任何错误消息,但在脚本完成后我没有得到任何zip文件。

目录结构:

└─# tree
.
├── Inbound
│   └── Lambda
│       ├── app-inbound
│       │   ├── outbound-primary.py
│       │   └── requirements.txt
│       └── app-outbound
│           ├── inbound-primary.py
│           └── requirements.txt
#!/bin/bash
start_dir=/tmp/Inbound
echo "start_dir=$start_dir"

for d in "$start_dir"/*/ ; do
   cd "$d" || exit
   dirs=(*/)
   declare -p dirs
   echo "Entering into location"
   for sub_dir in "${dirs[@]}"
   do
     echo "$sub_dir"
     zip "$sub_dir".zip "$sub_dir"
   done
done

If you have any specific questions or need further assistance, feel free to ask.

英文:

Trying to create zip file based on the directories, to initiate process declared as current directory based on dirs command. Then based on the dirs assigned the directory into array format. Using for loop (sub_dir) get the input data from dirs[@] as list and it will print the file name before creating zip file.

I don't see any error message while running the script but I'm not getting any zip file after the script completion.

Directory structure

└─# tree
.
├── Inbound
│   └── Lambda
│       ├── app-inbound
│       │   ├── outbound-primary.py
│       │   └── requirements.txt
│       └── app-outbound
│           ├── inbound-primary.py
│           └── requirements.txt
#!/bin/bash
start_dir=/tmp/Inbound
echo "start_dir=$start_dir"

for d in "$start_dir"/*/ ; do
   cd "$d" || exit
   dirs=(*/)
   declare -p dirs
   echo "Entring into location"
   for sub_dir in "${dirs[@]}"
   do
     echo "$sub_dir"
     zip "$sub_dir".zip "$sub_dir"
   done
done

答案1

得分: 2

你需要执行以下命令:

zip "${sub_dir%/}.zip" "$sub_dir"

因为如果你使用 bash -x 启用调试模式,你会看到:

+ zip app-inbound/.zip app-inbound/

这是一个参数扩展,用于删除末尾的 /

参考 parameter expansion

英文:

You need:

zip "${sub_dir%/}".zip "$sub_dir"

Because if you put debug mode with bash -x, you will see:

+ zip app-inbound/.zip app-inbound/

It's a parameter expansion to delete trailing /.

See parameter expansion

huangapple
  • 本文由 发表于 2023年5月18日 01:48:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274897.html
匿名

发表评论

匿名网友

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

确定