如何在脚本中使用循环从文件中一个一个地获取数据

how to use a loop to get data from file one by one in scripting

test abc bcd

所以我有一个名为 "password" 的文件,我试图一个一个地获取值来做一些测试。

#!/bin/bash
for i in '1..5' 
do
guess=`awk '{print $i}' password`
try=$(echo "$guess" | sha256sum)
testing="f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2"
if [" $try "==" $testing "]
then
    echo "the password is $guess"
else
    echo "password not found"
fi
done

所以我想使用这个 for 循环来获取值,但是我在 for 循环中遇到错误,我不知道如何修复它。

您在脚本中犯了错误,bash 默认情况下根据空格分隔,bash 中的字符串比较也不同于其他编程语言。

参考:http://www.tldp.org/LDP/abs/html/comparison-ops.html

此代码将解决您的问题。

#!/bin/bash
for guess in `cat password`; 
do
    try=$(echo "$guess" | sha256sum|awk '{print }')
    testing="f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2"
    if [ "$try" = "$testing" ]
    then
        echo "the password is $guess"
    else
        echo "password not found"
    fi
done