Search code examples
bashcsh

Convert script from csh to sh?


I'm trying to convert *.csh script to *.sh. I am having trouble specifically with this block.

foreach f (tmp*)
echo $f
set zid = `grep db $f`
if !(-e "$zid".mol2) then
set filename = "$zid".mol2
else foreach n (`seq -w 1 11247`)
if !(-e "$zid"_"$n".mol2) then
set filename = "$zid"_"$n".mol2     break
endif
end
endif
mv -v $f $filename
end

I tried changing it to this:

 for f in tmp*
 do
 echo $f
 set zid = `grep db $f`
 if ! [-e "$zid".mol2] ; then
 set filename = "$zid".mol2
 else for n in `seq -w 1 11247`
 if ! [-e "$zid"_"$n".mol2] ; then
 set filename = "$zid"_"$n".mol2
 break
 fi
 done
 fi
 mv -v $f $filename
 done

But I get this error:

 ./scripts/ex01.sh: line 31: syntax error near unexpected token `if'
 ./scripts/ex01.sh: line 31: `if ! [-e "$zid"_"$n".mol2] ; then'

Also I have a problem with this part cat $VSTROOT/db.mol2 | csplit -ftmp -n4 -ks - '%^@.TRIPOS.MOLECULE%' '/^@.TRIPOS.MOLECULE/' '{*}'

Where I preset VSTROOT=pwd, but when executed it tells me cat: pwd/db.mol2: No such file or directory.


Solution

  • Without really commenting on the propriety of this script, but just addressing the syntactic issues, the primary errors are incorrect assignments of variables, a missing "do", misuse/abuse of the [ command, and a few other details. Try:

        for f in tmp*
        do
            echo "$f"
            zid=$(grep db "$f")
            if ! [ -e "$zid".mol2 ] ; then
                filename="$zid".mol2
            else 
                for n in $(seq -w 1 11247)
                do
                    if ! [ -e "$zid"_"$n".mol2 ] ; then
                        filename="$zid"_"$n".mol2
                        break
                     fi
                done
            fi
            mv -v "$f" "$filename"
        done