Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:36:44+00:00 2026-05-30T08:36:44+00:00

When as part of shell script only one line is operating on a file

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:36:45+00:00Added an answer on May 30, 2026 at 8:36 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://bash.cyberciti.biz/file-management/shell-script-to-simulate-unix-more-command/ #!/bin/bash # Write a shell script like a more command. It asks the
I'm writing a quick shell script to build and execute my programs in one
I'm creating a shell script to a basic program. One option is to delete
I have to write a script to send mails using unix shell scripts. The
I have a shell script that calls a java jar file and runs an
I have following set of values in a tab delimited file (only a part
I'm trying to execute a command via ssh, take only the first part of
I'm trying to use PHP to create a shell script on Windows. It's part
I want to Run shell script using Cygwin in java.i want to add these
I am trying to run this shell script command through applescript on my mac

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.