首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在bash中调用在base64中解码的函数

在bash中调用在base64中解码的函数
EN

Stack Overflow用户
提问于 2021-03-02 05:50:05
回答 2查看 102关注 0票数 -1
代码语言:javascript
复制
#!/bin/bash

#if there are no args supplied exit with 1
if [ "$#" -eq 0 ]; then
        echo "Unfortunately you have not passed any parameter"
        exit 1
fi

#loop over each argument
for arg in "$@"
do
    if [ -f arg ]; then
                echo "$arg is a file."

                #iterates over the files stated in arguments and reads them    $
                cat $arg | while read line;
                do
                    #should access only first line of the file
                        if [ head -n 1 "$arg" ]; then
                                process line
                                echo "Script has ran successfully!"
                                exit 0
                        #should access only last line of the file
                        elif [ tail -n 1 "$arg" ]; then
                                process line
                                echo "Script has ran successfully!"
                                exit 0
                        #if it accesses any other line of the file
                        else
                                echo "We only process the first and the last line of the file."
                        fi
                done
        else
                exit 2
        fi
done

#function to process the passed string and decode it in base64
process()   {
        string_to_decode = "$1"
        echo "$string_to_decode = " | base64 --decode
}

基本上,我希望这个脚本做的是循环传递给脚本的参数,然后如果它是一个文件,那么调用在base64中解码的函数,但只在所选文件的第一行和最后一行进行解码。不幸的是,当我运行它时,即使调用了正确的文件,它也没有任何作用。我想它可能遇到了一些问题

代码的一部分。有什么想法吗?

编辑:所以我明白我实际上只是一遍又一遍地提取第一行,而不是真正地将它与任何东西进行比较。所以我试着把代码的if条件改成这样:

代码语言:javascript
复制
first_line = $(head -n 1 "$arg")
                    last_line = $(tail -n 1 "$arg")
                    if [ first_line == line ]; then
                            process line
                            echo "Script has ran successfully!"
                            exit 0
                    #should access only last line of the file
                    elif [ last_line == line ]; then
                            process line
                            echo "Script has ran successfully!"
                            exit 0

我的目标是遍历文件,例如,一个文件看起来像这样:

代码语言:javascript
复制
MTAxLmdvdi51awo=
MTBkb3duaW5nc3RyZWV0Lmdvdi51awo=
MXZhbGUuZ292LnVrCg==

并解码每个文件的第一行和最后一行。

EN

Stack Overflow用户

回答已采纳

发布于 2021-03-02 06:41:36

要解码提供给脚本的每个文件的第一行和最后一行,请使用以下命令:

代码语言:javascript
复制
#! /bin/bash  
for file in "$@"; do
  [ -f "$file" ] || exit 2
  head -n1 "$file" | base64 --decode
  tail -n2 "$file" | base64 --decode
done
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66430182

复制
相关文章

相似问题

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