首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Unrar和进度栏Bash

Unrar和进度栏Bash
EN

Stack Overflow用户
提问于 2015-09-01 22:13:36
回答 1查看 2.6K关注 0票数 0

我试图使用unrar命令提取目标目录中的RAR文件,但我不希望显示unrar在执行时显示的普通文件列表。相反,我要做的是显示一个进度条,它使用dialog --gauge以百分比显示进程,但到目前为止,我只得到了错误。

语法错误:文件的意外结束

下面是我使用的代码:

代码语言:javascript
复制
#!/bin/bash
dest="testing"
file"example.rar"
cmd="$(unrar x "$file" "$dest")" &>/dev/null

fc="$(unrar l "$file" | wc -l)" # get file count inside RAR file
fc="$((fc-10))" # file count has 10 extra lines, remove them
i=0  # index counter
(
  for f in "$cmd"; do
    pct="$(( 100*(++i)/fc ))"  # calculate percentage
    cat <<EOF
    XXX
    $pct
    Extracting file "$f"...
    XXX
    EOF
  done
) | dialog --title "Extracting Files" --gauge "Please wait" 7 70 0 

到目前为止,代码提取dest文件夹中的文件,但它们没有显示对话框,并在完成文件提取后显示错误unexpected end of file。它没有显示正在提取的任何文件,这是使用&>/dev/null所期望的,但我无法使对话框正确显示。

我从网上找到的各种例子中获得了这段代码,但到目前为止,我无法使它工作。有人能给我指明正确的方向吗?

EN

回答 1

Stack Overflow用户

发布于 2015-09-09 20:05:06

既然没人回答我的问题,我就自己回答。最后,我设法使程序工作,它提取文件,打印被提取文件的路径名,并在每个文件提取时正确显示表条填充。以下是代码:

代码语言:javascript
复制
#!/bin/bash
dest="testing" # destination directory
file"example.rar"

(
fc="$(unrar l "$file" | wc -l)" # get file count inside RAR file
fc="$((fc-10))" # file count has 10 extra lines, remove them
i=0  # index counter
unrar x "$file" "$dest" | while IFS="" read -r in ; do
  # extract the pathname of file being extracted
  f=$(echo $in | cut -d ' ' -f 2)

  echo "XXX"
  echo "$f..." # Display file name in the dialog box
  echo "XXX"
  echo "$pct %" # Show percent
  pct="$(( 100*(++i)/fc ))" # Calculate current percent
done
) | dialog --title "Extracting Files" --gauge "Please wait" 8 75 0

下一部分应该捕获执行.RAR文件提取的结果,但是我不确定此时是否只是捕获对话框的执行结果还是unrar命令的结果。我想要做的是控制脚本的流,以防unrar命令失败,我不完全确定我应该如何进行,但是现在,在我显示对话框的行之后,我放置了下面的if-那么:

代码语言:javascript
复制
if [ "$?" ]; then
  clear
  echo "Command successful"
fi

这是正确的,还是我应该将if-然后块移到代码中的其他地方?

同时,Imma以某种方式使这个脚本像一种修饰的unrar命令一样工作,您可以指定要提取的文件名和一个可选的目标目录,这样我就可以不需要指定目标目录了。

编辑:

这是我用来解决我自己问题的当前代码。到目前为止,代码似乎处理的错误还不错。我只测试了“文件已经存在”类型的错误,这些错误停止了脚本的执行以等待用户的输入,而CRC错误的情况是,您已经将压缩文件划分在不同的部分,其中一个文件丢失了。

代码语言:javascript
复制
#!/bin/bash
dest="testing" # destination directory
file="example.rar"

(
  fc="$(unrar l "$file" | wc -l)" # get file count inside RAR file
  fc="$((fc-10))" # file count has 10 extra lines, remove them
  i=0  # index counter

  # I have to redirect the output from 0 to 1 because when there's a
  # "file already exists" kind of error, unrar halts the execution of the
  # script to wait for user input, so I have to process that message inside
  # of the loop
  unrar x "$file" "$dest" 0>&1 2>&1 | while IFS="" read -r in ; do

    # we got a "file already exists" message?
    if [[ "$f" == *"already"* ]]; then
      echo "Error"  # display an error message
      break         # exit the while loop
    fi

    # extract the pathname of file being extracted
    f=$(echo $in | cut -d ' ' -f 2)

    echo "XXX"
    echo "$f..." # Display file name in the dialog box
    echo "XXX"
    echo "$pct %" # Show percent
    pct="$(( 100*(++i)/fc ))" # Calculate current percent
  done

  exit ${PIPESTATUS[0]} # exit with status message we got from unrar

) | dialog --title "Extracting Files" --gauge "Please wait" 8 75 0

# if PIPESTATUS[0] is not equal to zero, it means there was an error
if [[ "${PIPESTATUS[0]}" -ne 0 ]]; then
  echo "There was an error." # display a message
  exit 1  # exit with status 1
fi

# if command was successful, display a message  
if (( "$?" == 0 )); then
  echo "Command executed correctly $?"
fi

到目前为止,我没有对错误做任何事情,我只想检测它们并将消息返回到调用此错误的脚本中。它似乎运作得很好,但我知道必须有更好的方法来做到这一点。你的想法是什么?

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

https://stackoverflow.com/questions/32341691

复制
相关文章

相似问题

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