0

I have the following shell script:

#!/bin/sh echo "Configuring Xdebug" ip=10.0.2.2 xdebug_config="/etc/php/7.2/mods-available/xdebug.ini" echo "IP for the xdebug to connect back: ${ip}" echo "Xdebug Configuration path: ${xdebug_config}" echo "Port for the Xdebug to connect back: ${XDEBUG_PORT}" echo "Optimize for ${IDE} ide" if [ $IDE=='atom' ]; then echo "Configuring xdebug for ATOM ide" config="xdebug.remote_enable = 1 xdebug.remote_host=${ip} xdebug.remote_port = ${XDEBUG_PORT} xdebug.max_nesting_level = 1000 xdebug.remote_handler=dbgp xdebug.remote_mode=req xdebug.remote_autostart=true xdebug.remote_log=xdebug.log" # replace the file in $xdebug_config var except first line fi 

What I want is to replace the first line in the file mentioned in $xdebug_config variable EXCEPT the first line. For example if the file is:

line 1 line 2 somethig else lalala 

I want to get converted like that:

line 1 xdebug.remote_enable = 1 xdebug.remote_host=${ip} xdebug.remote_port = ${XDEBUG_PORT} xdebug.max_nesting_level = 1000 xdebug.remote_handler=dbgp xdebug.remote_mode=req xdebug.remote_autostart=true xdebug.remote_log=xdebug.log 

How I can achieve that?

Edit 1

As requested on the comments the $xdebug_config can contain theese possible values:

 /etc/php/7.2/mods-available/xdebug.ini /etc/php/5.6/mods-available/xdebug.ini /etc/php/7.0/mods-available/xdebug.ini 

Generally it will be in the following format:

 /etc/php/^number^.^number^/mods-available/xdebug.ini 

Edit 2

I perfected the shell script in order to be more clear.

3
  • Would it be possible that you include part of the file $xdebug_config in the question? This will make things much clearer. Currently, what you want is not clear, you had mentioned in the question What I want is to replace the first line in the file mentioned in $xdebug_config variable EXCEPT the first line. this is completely unclear. Commented Sep 12, 2018 at 19:23
  • 7
    Could you not just take the first line using head -n1 and then write out the rest according to what you want? Commented Sep 12, 2018 at 19:27
  • 2
    take care: this will always be true: [ $IDE=='atom' ] -- paste your code into shellcheck.net for more details. Commented Sep 12, 2018 at 19:50

4 Answers 4

2

How about a HERE doc?

line1=$(head -1 "$1") cat <<EORL >"$1" ${line1} this is line2 how about this for line3? let's do another line! moving on to the next line Wait! There's more??? EORL exit 0 
0
1

To replace a file's contents with arbitrary known data, but preserve the first line, you can do something like:

oldfile="/path/to/original/file" newfile="$(mktemp)" head -n1 "$oldfile" > "$newfile" cat << EOF >> "$newfile" Hey, all of these lines? The lines after "cat"? All of these lines up to and excluding the next line will be written. EOF mv "$oldfile" "${oldfile}.old" mv "$newfile" "$oldfile" 

The advantage of crafting a new file and moving it into place after it's fully composed is that you can keep the last version in case you need to roll back.

If you have no interest in doing this, you can just blow the old file away, but you can't read from and write to it in the same operation, so something like this will work:

header="$(head -n1 /path/to/file)" echo "$header" > /path/to/file cat << EOF >> /path/to/file Hey, all of these lines? The lines after "cat"? All of these lines up to and excluding the next line will be written. EOF 
1

I'd write

{ sed 1q "$xdebug_config"; echo "$config"; } | sponge "$xdebug_config" 

sponge is in the moreutils package. If you don't want to install that:

tmp=$(mktemp) { sed 1q "$xdebug_config"; echo "$config"; } > "$tmp" && mv "$tmp" "$xdebug_config" 
0

you can do it as like this :

tempd=$(mktemp -d);printf '%s\n' "$config" > "$tempd/config" sed -i -e "r $tempd/config" -e q "$xdebug_config" rm -rf "$tempd" 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.