LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed not working in bash script (https://www.linuxquestions.org/questions/programming-9/sed-not-working-in-bash-script-4175734593/)

Linux_Kidd 03-05-2024 03:39 PM

sed not working in bash script
 
This line works from bash shell cli.
GNU bash, version 5.0.0(1)

But not if I place it into a bash script.

In cli is does just that, adds START END after permissions tag
In script, nothing happens.

Bad syntax inside a script?

sed -i -e "/\<permissions\>/a \<\!-- START -->\n\<\!-- END --\>" myfile

astrogeek 03-05-2024 04:54 PM

Quote:

Originally Posted by Linux_Kidd (Post 6487864)
This line works from bash shell cli.
GNU bash, version 5.0.0(1)

But not if I place it into a bash script.

In cli is does just that, adds START END after permissions tag
In script, nothing happens.

Bad syntax inside a script?

sed -i -e "/\<permissions\>/a \<\!-- START -->\n\<\!-- END --\>" myfile

It may be helpful to show how it is used in your script. A quick test here works fine,

Some general comments, not specifically related to the script context...

From your description I think the escaped brackets probably have an unintended effect as they will match an empty string at those positions, not the opening and closing brackets.

I would suggest using single quotes to tone down shell expansions and all those backslashes are probably not needed either.

Code:

$ cat infile
<tag1>
<permissions>
</permissions>
</tag1>

$ sed '/<permissions>/a <!-- START -->\n<!-- END -->' infile
<tag1>
<permissions>
<!-- START -->
<!-- END -->
</permissions>
</tag1>

$ sed '/\<permissions\>/a <!-- START -->\n<!-- END -->' infile
<tag1>
<permissions>
<!-- START -->
<!-- END -->
</permissions>
<!-- START -->
<!-- END -->
</tag1>

Note the effect of the escaped brackets in the second expression.

Either of these work in a simple script here, so it may be your script or environment preventing success on your end, so more info would be helpful.

Linux_Kidd 03-05-2024 05:04 PM

Quote:

Originally Posted by astrogeek (Post 6487872)
It may be helpful to show how it is used in your script. A quick test here works fine,

Some general comments, not specifically related to the script context...

From your description I think the escaped brackets probably have an unintended effect as they will match an empty string at those positions, not the opening and closing brackets.

I would suggest using single quotes to tone down shell expansions and all those backslashes are probably not needed either.

script
Code:

#! /system/bin/bash

sed -i -e "/\<permissions\>/a \<\!-- START -->\n\<\!-- END --\>" myfile

I escaped < > and ! with backslash , without that it seemed to have issue on cli

It's Android OS linux, but bash should be fine.

goumba 03-05-2024 05:15 PM

Quote:

Originally Posted by astrogeek (Post 6487872)

From your description I think the escaped brackets probably have an unintended effect as they will match an empty string at those positions, not the opening and closing brackets.

Note the effect of the escaped brackets in the second expression.

The second expression matches the permission in "</permission>" because the <> and / are non word characters. The escaped <> work as a kind of more side-specific \b.

https://stackoverflow.com/questions/...pressions-vs-b

Linux_Kidd 03-05-2024 06:08 PM

Quote:

Originally Posted by goumba (Post 6487875)
The second expression matches the permission in "</permission>" because the <> and / are non word characters. The escaped <> work as a kind of more side-specific \b.

https://stackoverflow.com/questions/...pressions-vs-b

The pattern match should be a literal <permissions> , that's a tag in XML I need to append after.

Running it from cli I end up with
<permissions>
<!-- START -->
<!-- END -->

astrogeek 03-05-2024 10:39 PM

Quote:

Originally Posted by Linux_Kidd (Post 6487882)
The pattern match should be a literal <permissions> , that's a tag in XML I need to append after.

Running it from cli I end up with
<permissions>
<!-- START -->
<!-- END -->

That is what I thought you intended, so you do not want to put a backslash before the brackets.

I am not familiar with the Android environment so perhaps someone else can explain the failure in script problem.

NevemTeve 03-06-2024 12:27 AM

@OP How did you test your script? There might have been error messages you didn't see. Lack of write-access-right on the file, for example.

pan64 03-06-2024 12:41 AM

the most important things are:
1. add some error handling, and some checks in your script
2. use a log file to store those information
And you will see if (for example) file was not found, or sed was not found or anything else happened

Linux_Kidd 03-06-2024 07:24 AM

Quote:

Originally Posted by astrogeek (Post 6487906)
That is what I thought you intended, so you do not want to put a backslash before the brackets.

I am not familiar with the Android environment so perhaps someone else can explain the failure in script problem.

With the backslash before < and > , it has no effect, at least not on cli. I did not try w/o backslash in script, but will try that.

Quote:

Originally Posted by pan64 (Post 6487920)
the most important things are:
1. add some error handling, and some checks in your script
2. use a log file to store those information
And you will see if (for example) file was not found, or sed was not found or anything else happened

Yeah, that's next step. It's just odd it works 100% from cli, but not when shoved into a script that runs via same bash shell.

danielbmartin 03-06-2024 09:16 AM

Quote:

Originally Posted by Linux_Kidd (Post 6487980)
my salary sucks, 2¢/sec

My (admittedly fallible) arithmetic says this works out to almost $150K/year. Not too shabby...

Daniel B. Martin

.

Linux_Kidd 03-06-2024 09:32 AM

Quote:

Originally Posted by danielbmartin (Post 6487997)
my (admittedly fallible) arithmetic says this works out to almost $150k/year. Not too shabby...

Daniel b. Martin

.


:)


Yeah, but let me tell you why it sucks. I had that salary for past 20yrs, and I have been with only a few companies over that period, and I have advanced my skillsets and have made technical contributions to security industry! Cheap [add your fav curse word here] companies.

Linux_Kidd 03-06-2024 09:37 AM

With some logging and looking, found my err.

There were no errors from commands.
Turns out one of my sed commands was doing what it said it should do, which was not what I needed it to do.
I however thought I was testing the sequence from script on cli, but maybe I wasn't. I am still thinking about that.
I reworked one sed command and now all is ok.
The error was happening prior to the sed command I listed in post #3.
solved.

btw, sed on Android is a link to toybox, as there is no sed binary on the system.

chrism01 03-12-2024 11:25 PM

Quote:

Turns out one of my <blah> commands was doing what it said it should do, which was not what I needed it to do.
;) :)

You'd be amazed how often that turns out to be the problem - in any language ..

pan64 03-13-2024 01:34 AM

Quote:

Originally Posted by Linux_Kidd (Post 6487999)
Turns out one of my sed commands was doing what it said it should do, which was not what I needed it to do.

I saw this statement somewhere:
Quote:

A program will never do what you wish but what was implemented!


All times are GMT -5. The time now is 07:18 PM.