sh read command eats backslashes in input?

Accrding to: http://www.vias.org/linux-knowhow/bbg_sect_08_02_01.html : -r If this option is given, backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslash-newline pair may not be used as a line continuation. It works on my machine. $ echo ‘\&|’ | while read -r in; do … Read more

Is there an elegant way to store and evaluate return values in bash scripts?

The simple solution: output=$(complex_command) status=$? if (( status == 0 )); then : stuff with “$output” fi : more code if (( status == 0 )); then : stuff with “$output” fi Or more eleganter-ish do_complex_command () { # side effects: global variables # store the output in $g_output and the status in $g_status g_output=$( … Read more

how to edit XML using bash script?

To change tag‘s value to 2 and tag1‘s value to 3, using XMLStarlet: xmlstarlet ed \ -u ‘/root/tag’ -v 2 \ -u ‘/root/tag1’ -v 3 \ <old.xml >new.xml Using your sample input: xmlstarlet ed \ -u ‘/root/tag’ -v 2 \ -u ‘/root/tag1’ -v 3 \ <<<‘<root><tag>1</tag><tag1>2</tag1></root>’ …emits as output: <?xml version=”1.0″?> <root> <tag>2</tag> <tag1>3</tag1> </root>

Compare a string using sh shell

You should use the = operator for string comparison: Sourcesystem=”ABC” if [ “$Sourcesystem” = “XYZ” ]; then echo “Sourcesystem Matched” else echo “Sourcesystem is NOT Matched $Sourcesystem” fi; man test says that you use -z to match for empty strings.

How can I split a shell command over multiple lines when using an IF statement?

The line-continuation will fail if you have whitespace (spaces or tab characters¹) after the backslash and before the newline. With no such whitespace, your example works fine for me: $ cat test.sh if ! fab –fabfile=.deploy/fabfile.py \ –forward-agent \ –disable-known-hosts deploy:$target; then echo failed else echo succeeded fi $ alias fab=true; . ./test.sh succeeded $ … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)