我正在编写下面的脚本,但它似乎不起作用,因为这是输出:
## entered first loop ##
[==1.0]: command not found
[==2.0]: command not found
## entered first loop ##
[==1.0]: command not found
[==2.0]: command not found
## entered first loop ##
[==1.0]: command not found
[==2.0]: command not found
## entered first loop ##
[==1.0]: command not found
[==2.0]: command not found
这是我的脚本:
#!/bin/bash
declare -a arr1=("1000/1.0" "2000/2.0" "3000/3.0" "4000/4.0") &&
declare -a arr2=("100/0.138" "200/0.098" "300/0.07347" "400/0.0545" "500/0.0449") &&
for i in "${arr1[@]}" &&
do &&
echo -e "\e[41m## entered first loop ##\e[0m"
str1=$i | cut -d'/' -f1 &&
str2=$i | cut -d'/' -f2 &&
if [str2="1.0"] &&
then &&
sed -i 's/1.0/1.0/g' file1.txt &&
else &&
sed -i 's/$ii/$str2/g' file1.txt &&
fi &&
for j in "${arr2[@]}" &&
do &&
echo -e "\e[41m## entered second loop ##\e[0m"
str3=$j | cut -d'/' -f1 &&
str4=$j | cut -d'/' -f2 &&
old_loc="test_name=name_([^]+)_ni_([^]+)" &&
new_loc="test_name=name_$str1_ni_str3" &&
sed -i 's/old_loc/new_loc/g' file2.sh &&
old_loc2="#hello HELLO ([^]+) // 4 am" &&
new_loc2="#hello HELLO $str4 // 4 am" &&
sed -i 's/old_loc2/new_loc2/g' file.cpp &&
done
ii=$str2 &&
if [str2="2.0"] &&
break
fi
done
答案1
我修复了文件中的几个语法错误,并删除了所有&&
s。主要问题是数组声明和 IF 语句。结果如下:
#!/bin/bash
declare -a arr1=("1000/1.0", "2000/2.0", "3000/3.0", "4000/4.0")
declare -a arr2=("100/0.138", "200/0.098", "300/0.07347", "400/0.0545", "500/0.0449")
for i in "${arr1[@]}"
do
echo -e "\e[41m## entered first loop ##\e[0m"
str1=$i | cut -d'/' -f1
str2=$i | cut -d'/' -f2
if [ "$str2" = "1.0" ]
then
sed -i 's/1.0/1.0/g' file1.txt
else
sed -i 's/$ii/$str2/g' file1.txt
fi
for j in "${arr2[@]}"
do
echo -e "\e[41m## entered second loop ##\e[0m"
str3=$j | cut -d'/' -f1
str4=$j | cut -d'/' -f2
old_loc="test_name=name_([^]+)_ni_([^]+)"
new_loc="test_name=name_$str1_ni_str3"
sed -i 's/old_loc/new_loc/g' file2.sh
old_loc2="#hello HELLO ([^]+) // 4 am"
new_loc2="#hello HELLO $str4 // 4 am"
sed -i 's/old_loc2/new_loc2/g' file.cpp
done
ii=$str2
if [ "$str2" = "2.0" ]
then
break
fi
done