while i want to execute this script, the execution was blocked at the cut command and the man cut was displayed
the script code
#!/bin/bash
for i in `cat newcontext` ;do
var1=`cut –f1 –d" " $i`
var2=`cut –f2 –d" " $i`
if [ $var2 = false ];then
for j in `cat adaptrules`;do
c=`cut -f1 -d" " $j`
cc=`cut -f2 -d" " $j`
if [ $c = $var1 ];then
r=$cc
fi
done
sed /$var1/d currentconfig>>newconfig
else
for k in `cat adaptrules`;do
var3=`cut –f1 –d" " $k`
var4=`cut –f2 –d" " $k`
if [ $var3 = $var1 ];then
action=$var4
fi
done
cat $action >> newconfig
fi
done
It's difficult to know if you are trying to read from a file named in the variables i
, j
, and k
, or if you are trying to just parse the lines of newcontext
and adaptrules
. In either case you should simply not use cut
at all. If you are attempting the latter, you can instead do something like:
#!/bin/bash
while read var1 var2 ; do
if test "$var2" = false; then
while read c cc ; do
if test "$c" = "$var1"; then
r=$cc
fi
done < adaptrules
<&- sed /$var1/d currentconfig>>newconfig #WTF: multiple iterations with only one line omitted?
else
while read var3 var4 ; do
if test "$var3" = "$var1"; then
action=$var4
fi
done < adaptrules
<&- cat $action >> newconfig # Does $action really name a file?
# I'm guessing this should be 'echo'
fi
done < newcontext
I find the formatting of the code in the question makes it difficult to read, so I will not spend a lot of time verifying that this logic matches yours. It appears that the variable r
is totally unused, the sed
and the cat
seem incorrect...but this gives you the idea.
Also, it might be stylistically cleaner to write the inner portion as:
if test "$var2" = false; then
while read c cc; do
...
done
else
while read var3 var4; do
...
done
...
fi < adaptrules
Note that you need to be careful that none of the commands inside the outer while loop consume anything from stdin. (hence the <&-
which closes that file descriptor for sed
and cat
, which is not strictly necessary in these cases but demonstrates how to ensure that you do not inadvertently consume from the input to the loop.) It's probably cleaner to do things like:
while read -u 5 var1 var2; do
...
done 5< newcontext