简体   繁体   中英

erreur commande cut script shell

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

the content of text files

my script generates a new configuration (newconfig) using the context data (newcontext), the current configuration and adaptations rules.

You would need to execute the cut command with example:

var1=$(cut -f1 -d " " <<< $i)

You are trying to execute the cut command as if $i contains a filename when it actually contains text.

my script is intended to generate a new configuration (newconfig). this configuration is generated using the context data (new context), adaptations rules (adaptrules) and the current configuration (currentconfig). the script works as follow: for each contextdata (the field var1 in newcontext), we look for its action (the field cc in adaptrules). then we verify if this action is present in the currentconfiguration. then, if the action of the selected contextdata is present in the currentconfig and the field var2 of contextdata is equal to false, then the action is deleted from the currentconfig else if the action is absent in the currentconfig and the var2 of contextdata is equal to true, so the action is added to the currentconfig. I had modified my script but it generated an error "sed, command c expects followed by text".

the new code is as follows

#!/bin/bash

while read var1 var2 ;do

while read c cc;do
if test "$c" = "$var1" ;then
r=$cc
fi
done <adaptrules

exist=false
while read var; do

if test "$var" = "$r";then
exist=true 
fi
done < currentconfig
if test "$var2" = false  && test "$exist" = true; then

sed -i "/$r/d" currentconfig

fi

if test "$var2" = true && test "$exist" = false; then

echo "$r">> currentconfig

fi

done < newcontext

thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM