shell if [] 条件 and read

来源:互联网 发布:同信证券交易软件 编辑:程序博客网 时间:2024/04/26 12:34

 Recent week  I deal with the shell script met many problem.

 firstly  ksh has some grammar different with bash,so you much well know what sh type you deal with.

 

 about ksh if condition should take care of

 

  if [[ $line == $xslSignBegin ]]; then

  echo "$line"

  fi

1. there is space between if and "[[" ,  operators and "==" , "[[ "and operators,"]] "and operator ,";" and then

(so many spaces)

2.use "=" if you don't have space in operator . and bash use = not ==

 

 the following three days I done a shell

 if you met a problem between like test:unknown operator try ' '

 

 

#!bin/ksh

 

mozillaDir=/usr/share/mime/packages
fileName=freedesktop.org.xml
fileNameBack=freedesktop.org.xmlback

if [ ! -f $mozillaDir/$fileNameBack ]; then
 
 if [ 'id 2> /dev/null | cut -d"=" -f2 2> /dev/null | cut -d"(" -f1 2> /dev/null' ]; then
  cp $mozillaDir/$fileName   $mozillaDir/$fileNameBack
  xslSignBegin="<mime-type type=/"text/x-xslt/">"
  commentBegin="<!--"
  commentEnd="-->"
  xslSignEnd="</mime-type>"
  isEdit=false
  addComment=false
  while read line
  do
         
    if [[ $line == $xslSignBegin ]]; then
     isEdit=true
     addComment=true
     line=$commentBegin$line
      
    elif [[ $addComment = true && $line == $xslSignEnd ]]; then
     line=$line$commentEnd
     addComment=false
    fi
    echo "$line" >> $mozillaDir/temp
  done < $mozillaDir/$fileName
  if [[ $isEdit = true ]]; then
   mv $mozillaDir/$fileName   $mozillaDir/$fileName"update"
   mv $mozillaDir/temp   $mozillaDir/$fileName
   rm $mozillaDir/temp
   isEdit=false
      echo "exectue command update"
   update-mime-database /usr/share/mime
  fi
 else
  echo "You should switch root user to config Mozilla environment to support text//xsl MIME  type or change  firefox browser in order to run successfully!"
 fi
fi