gcc|C|bash|cat|sed|perl: Bad file descriptor
Von: , Frage gestellt am Do, 8. Mär 2007
Guten Tag
Nach dem, mit ganz untenstehendem Bash-Script, einige *.h und ".C Dateien manipuliert wurden, erhalte ich folgenden Fehler beim Kompilieren mit gcc:
*.h:22:27: calling fdopen: Bad file descriptor gmake[1]: *** [*.o] Error 1 gmake[1]: Leaving directory `/home/myself/cpp/cprog1' make: *** [all] Error 2Komischerweise tritt dieser Fehler bei Dateien auf, die nicht mit dem Script manipuliert wurden.
Woran kann das liegen?
Hier noch ein vorangehender Artikel dazu: http://www.wer-weiss-was.de/cgi-bin/forum/showarchiv...
Dank und Gruss
Olli
#!/bin/bash --init-file
#
#
# function add_macro_to_macroh
#
function add_macro_to_macroh()
{
echo ''
MACROH='/macro/macro.h'
MACROH_NEW=$MACROH.new
checkout $MACROH > /dev/null
cat $MACROH | grep -v '#endif' >& $MACROH_NEW
cat >> $MACROH_NEW << lblCAT
/**
* +++ String Copy Macro +++
**/
#define STRCPY(DEST,SRC) if(DEST!=SRC) strcpy(DEST,SRC)
#endif
lblCAT
mv $MACROH_NEW $macroh
echo added macro to $MACROH
}
#
# function add_macroh_include_to_file
#
function add_macroh_include_to_file()
{
echo 'add_macroh_include_to_file' $1
$ perl -i.bak -0777 -pe '
my @p;/#include\s+["<]\w+\.?\w+[">]\s*\n(?{push@p,pos})/g;
substr($_,pop@p,0,qq{#include "macroh.h"\n}) $1
'
}
#
# function compile_file
#
function compile_file()
{
COMPILE="${CC} ${CCOPTS} ${CINCLUDES} -c"
echo 'compile' $1
$COMPILE $1 >& /dev/null
if [[ $? != 0 ]]
then
echo 'error while compiling' $1
add_macroh_include_to_file $1
echo 'compile again' $1
$COMPILE $1 >& /dev/null
if [[ $? != 0 ]]
then
echo 'error while compiling' $1
revert $1 >& /dev/null
echo 'reverted' $1
else
echo 'compilation successful for' $1
fi
else
echo 'compilation successful for' $1
fi
}
#
# function replace_and_compile
#
function replace_and_compile()
{
for arg in "$@"; do
echo ''
#
# search and replace and save changes
#
echo 'sed "s/strcpy/STRCPY/g"' $arg '>' $arg.new
sed 's/strcpy/STRCPY/g' $arg > $arg.new
#
# move changes back to original file
#
echo 'mv' $arg.new $arg
mv $arg.new $arg
#
# remove *.new file
#
echo 'rm -f' $arg.new $arg
rm -f $arg.new
#
# compile file
#
compile_file $arg
done
}
#
# Start
#
echo ''
echo 'starting replacement of strcpy with STRCPY'
# set environment
source ~/.bashrc
setmyenv
#
# add the macro to macroh.h
#
add_macro_to_macroh
#
# make files editable, replace string and compile
#
echo ''
echo 'checkout files'
checkout $(find_in_file strcpy h C | grep -v _gui) >& /dev/null
echo ''
echo 'start replacement of strcpy with STRCPY and compile'
replace_and_compile $(find_in_file strcpy h C | grep -v _gui)
#
# End
#
echo ''
echo 'done'
echo 'please replace strcpy manualy in reverted files'
echo 'please submit files if everything is okay'
#
# revert where STRCPY, for convenience only
#
# echo ''
# echo 'Revert where STRCPY'
# revert $(find_in_file STRCPY h C | grep -v _gui) >& /dev/null
#
# revert where strcpy, for convenience only
#
# echo ''
# echo 'Revert where strcpy'
# revert $(find_in_file strcpy h C | grep -v _gui) >& /dev/null
#
# Exit Shell
#
exit
