bash script append text to first line of a file
To edit a file, you can use ed
, the standard editor:
line=' bcm2708.w1_gpio_pin=20'file=/boot/cmdline.txtif ! grep -q -x -F -e "$line" <"$file"; then ed -s "$file" < <(printf '%s\n' 1 a "$line" . 1,2j w q)fi
ed
's commands:
1
: go to line 1a
: append (this will insert after the current line)- We're in insert mode and we're inserting the expansion of
$line
.
: stop insert mode1,2j
join lines 1 and 2w
: writeq
: quit