#!/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条件改成这样:
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我的目标是遍历文件,例如,一个文件看起来像这样:
MTAxLmdvdi51awo=
MTBkb3duaW5nc3RyZWV0Lmdvdi51awo=
MXZhbGUuZ292LnVrCg==并解码每个文件的第一行和最后一行。
发布于 2021-03-02 06:41:36
要解码提供给脚本的每个文件的第一行和最后一行,请使用以下命令:
#! /bin/bash
for file in "$@"; do
[ -f "$file" ] || exit 2
head -n1 "$file" | base64 --decode
tail -n2 "$file" | base64 --decode
done发布于 2021-03-02 06:23:35
是的,正如其他人已经说过的那样,脚本的真正目标并不是很清楚。也就是说,我想你可能想要做的每一种变化都会被这样的东西覆盖:
#!/bin/bash
process() {
encoded="$1";
decoded="$( echo "${encoded}" | base64 --decode )";
echo " Value ${encoded} was decoded into ${decoded}";
}
(( $# )) || {
echo "Unfortunately you have not passed any parameter";
exit 1;
};
while (( $# )) ; do
arg="$1"; shift;
if [[ -f "${arg}" ]] ; then
echo "${arg} is a file.";
else
exit 2;
fi;
content_of_first_line="$( head -n 1 "${arg}" )";
echo "Content of first line: ${content_of_first_line}";
process "${content_of_first_line}";
content_of_last_line="$( tail -n 1 "${arg}" )";
echo "Content of last line: ${content_of_last_line}";
process "${content_of_last_line}";
line=""; linenumber=0;
while IFS="" read -r line; do
(( linenumber++ ));
echo "Iterating over all lines. Line ${linenumber}: ${line}";
process "${line}";
done < "${arg}";
done;您可能会发现一些有用的附加功能:
如果使用多个文件名调用脚本,假设有4个不同的文件名,而第二个文件不存在(但其他文件存在),您真的希望脚本:处理第一个文件,然后注意到第二个文件不存在,并在该点退出吗?而不处理(可能有效的)第三和第四个文件?替换该行:
exit 2;使用
continue;将使它跳过任何无效的文件名,并仍然处理之后的有效文件名。
此外,在您的
流程
函数,紧跟在该行之后:
decoded="$( echo "${encoded}" | base64 --decode )";您可以在回显之前检查解码是否成功,无论结果是什么,如果行不是有效的base64,那么结果可能是垃圾。
if [[ "$?" -eq 0 ]] ; then
echo " Value ${encoded} was decoded into ${decoded}";
else
echo " Garbage.";
fi;--
为了回答您关于IFS/read-construct的后续问题,它混合了几个组件:
read -r line从输入中读取一行(-r告诉它不要做任何奇特的反斜杠转义魔术)。
while ... ; do ... done ;这个while循环围绕着read语句,因此我们不断重复读取一行的过程,直到用完为止。
< "${arg}";这会将文件名$arg的内容作为输入输入到整个代码块中(因此,这将成为
阅读
语句读取自)
IFS=""这告诉read语句使用空值而不是真正的内置IFS值(内部字段分隔符)。通常,对每个read语句都这样做是一个好主意,除非您有一个需要将行拆分为多个字段的用例。
如果不是
IFS="" read -r line你要用的是
IFS=":" read -r username _ uid gid _ homedir shell并从/etc/passwd中读取,该文件包含如下代码行:
root:x:0:0:root:/root:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin然后,该IFS值将允许它将这些值加载到正确的变量中(换句话说,它将拆分为":")
IFS的默认值是从您的shell继承的,它通常包含空格和TAB字符,可能还有其他一些东西。当您只读入一个变量时(在本例中为$line)。IFS=不会被应用,但是当你改变一个read语句并添加另一个变量时,分词开始生效,缺少本地IFS值将使完全相同的脚本在不同的情况下表现得非常不同。因此,在任何时候控制它都是一个很好的习惯。
引用" $arg“或"${arg}”之类的变量,而不是引用$arg,也是如此。当ARG=说“hello”时,这并不重要;但是一旦值突然包含空格,所有的事情都会变得不同;惊喜从来都不是一件好事。
https://stackoverflow.com/questions/66430182
复制相似问题