Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can someone help me understand this script? It is from sysconf_add and I am new to scripting.
I need to do something similar.

function add_word() {  
    local word=$1  
    local word_quoted=$2  
    if ! word_present; then  
        $debug && cp $file $tmpf  
        sed -i -e "${lineno} {   
            s/^[[:space:]]*\($var=\".*\)\(\".*\)/\1 $word_quoted\2/;   
            s/=\" /=\"/   
            }" $file  
        $debug && diff -u $tmpf $file  
        echo \"$word\" already present  
    # some balancing for vim"s syntax highlighting  
share|improve this question
Maybe ask a more specific question? Which line you do not understand ? Also, you should give complete references (What is sysconf_add? What operating system? What shell does it uses?). – vladmihaisima Oct 22 '12 at 9:14
@vladmihaisima:I am interested in sed.I don't really know what is…. I think it is for apache – Jim Oct 22 '12 at 9:20

This is a good resource for sed

For your example above:

  • The -i flag means 'edit in-place' (GNU sed only), this is editing $file in-place
  • The -e flag chains together multiple commands.
  • The first command in your example looks to be replacing instances of $var="<stuff>" with the second argument passed to the script.
  • The second command removes trailing space following the sequence =" (e.g. foo=" bar " would become foo="bar ")
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .