Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple sed operations creating empty file

Tags:

shell

ksh

sed

When as part of shell script only one line is operating on a file using sed command the redirected file contains the updated data, as below

cat ${PROP_PATH}/${PROP_FILE} | sed "s!${ISTR_KEY}=.*!${ISTR_KEY}=${SIM_ISTR_KEY_VAL}!" > ${UPDATEDPROPS_DIR}/${PROP_FILE}

whereas when it is executed as part of a shell script, where after this another sed command updates the same file as in the below script at the end what i get is an empty file, why ? ..... ideas please.

(check 'switchAll2Sim()' function below)

#!/bin/ksh
#
SIM_ICR_KEY_VAL="http://www.example.com/sim/http/icr"
SIM_ISTR_KEY_VAL="http://www.example.com/sim/http/istr"
SIM_GT_KEY_VAL="http://www.example.com/sim/http/gtr"
#
ICR_KEY="interface.url.icr"
ISTR_KEY="interface.url.istr"
GT_KEY="interface.ws.url.gt"
## Property Files
PROP_PATH=""
PROP_FILE="properties"
##
DATE=`date +%m%d%Y`
DATETIME=`date +%m%d%Y-%T`
BCKUP_DIR=_bckup
UPDATEDPROPS_DIR=_updatedprops
# ----------------------------------
pause(){
echo "Press [Enter] key to continue..."
  read fackEnterKey
}

permissions(){
    chmod 777 ${UPDATEDPROPS_DIR}
}

backup(){

    if [ ! -d "${BCKUP_DIR}" ]; then
    mkdir   ${BCKUP_DIR}
    fi

    if [ ! -d "${UPDATEDPROPS_DIR}" ]; then
    mkdir   ${UPDATEDPROPS_DIR}
    fi

    permissions

    ## keep backup of properties
    cp ${PROP_PATH}/${PROP_FILE} ${BCKUP_DIR}/${PROP_FILE}_${DATETIME}

    echo "Backup of property files completed at: " ${DATETIME}
}

#-------------------------------------------------------------
# switch all properties to SIM
#------------------------------------------------------------- 
switchAll2Sim(){

    backup

    #
    # update files

    cat ${PROP_PATH}/${PROP_FILE} | sed     "s!${ISTR_KEY}=.*!${ISTR_KEY}=${SIM_ISTR_KEY_VAL}!" > ${UPDATEDPROPS_DIR}/${PROP_FILE}
    cat ${UPDATEDPROPS_DIR}/${PROP_FILE} | sed "s!${ICR_KEY}=.*!${ICR_KEY}=${SIM_ICR_KEY_VAL}!" > ${UPDATEDPROPS_DIR}/${PROP_FILE}
    cat ${UPDATEDPROPS_DIR}/${PROP_FILE} | sed "s!${GT_KEY}=.*!${GT_KEY}=${SIM_GT_KEY_VAL}!" > ${UPDATEDPROPS_DIR}/${PROP_FILE}

    echo "Switch all to SIM completed at: " ${DATETIME}

  pause
}

# switch all properties to real 
#-------------------------------------------------------------
switchAll2Real(){   
  pause
}
#-------------------------------------------------------------
dispCurrentStats(){

    echo "Displaying current properties..."
    echo "*********************************"
    echo "  File: " ${PROP_PATH}/${PROP_FILE}
    grep ${ICR_KEY} ${PROP_PATH}/${PROP_FILE}
    grep ${ISTR_KEY} ${PROP_PATH}/${PROP_FILE}
    grep ${GT_KEY} ${PROP_PATH}/${PROP_FILE}
    #
    echo "*********************************"
    pause
}

show_menus() {
    clear
    echo "~~~~~~~~~~~~~~~~~~~~~"    
    echo " M E N U"
    echo "~~~~~~~~~~~~~~~~~~~~~"
    echo "1. Display current properties"
    echo "2. Switch all to real"
    echo "3. Switch all to simulator"
    echo "4. Exit"

}

# read input from the keyboard and take a action
read_options(){

    read option
    case $option in
        1) dispCurrentStats ;;
        2) switchAll2Real ;;
        3) switchAll2Sim ;;
        4) exit 0;;
        *) echo "Please insert options 1 ~ 4";;
    esac
}

# -----------------------------------
# Main - infinite loop
# ------------------------------------
while true
do
    show_menus
    read_options
done
like image 432
sid Avatar asked May 26 '26 10:05

sid


1 Answers

Thanks, using '-i, says [sed: illegal option -- i]

Then you have to work with tmp files.

cp foo foo.tmp 
sed "s/x/y/" foo.tmp > foo
/bin/rm foo.tmp

OR

sed "s/x/y/" foo > foo.tmp
/bin/mv -f foo.tmp foo

is probably more efficient.

I hope this helps.

like image 146
shellter Avatar answered May 30 '26 05:05

shellter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!