LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2024, 03:39 PM   #1
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Rep: Reputation: 78
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
 
Old 03-05-2024, 04:54 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,266
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Quote:
Originally Posted by Linux_Kidd View Post
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.
 
Old 03-05-2024, 05:04 PM   #3
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by astrogeek View Post
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.
 
Old 03-05-2024, 05:15 PM   #4
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Quote:
Originally Posted by astrogeek View Post

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
 
Old 03-05-2024, 06:08 PM   #5
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by goumba View Post
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 -->

Last edited by Linux_Kidd; 03-05-2024 at 06:11 PM.
 
Old 03-05-2024, 10:39 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,266
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Quote:
Originally Posted by Linux_Kidd View Post
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.
 
Old 03-06-2024, 12:27 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,865
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@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.

Last edited by NevemTeve; 03-13-2024 at 02:41 AM.
 
Old 03-06-2024, 12:41 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
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
 
Old 03-06-2024, 07:24 AM   #9
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by astrogeek View Post
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 View Post
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.

Last edited by Linux_Kidd; 03-06-2024 at 07:26 AM.
 
Old 03-06-2024, 09:16 AM   #10
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Linux_Kidd View Post
my salary sucks, 2¢/sec
My (admittedly fallible) arithmetic says this works out to almost $150K/year. Not too shabby...

Daniel B. Martin

.
 
Old 03-06-2024, 09:32 AM   #11
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by danielbmartin View Post
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.

Last edited by Linux_Kidd; 03-06-2024 at 09:41 AM.
 
Old 03-06-2024, 09:37 AM   #12
Linux_Kidd
Member
 
Registered: Jan 2006
Location: USA
Posts: 737

Original Poster
Rep: Reputation: 78
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.

Last edited by Linux_Kidd; 03-06-2024 at 10:09 AM.
 
Old 03-12-2024, 11:25 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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 ..
 
Old 03-13-2024, 01:34 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Quote:
Originally Posted by Linux_Kidd View Post
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed script and sed command line give different results no-windose Linux - Newbie 7 01-09-2023 04:19 AM
[SOLVED] [BASH / SED] sed executed in script does not work czezz Programming 5 06-24-2020 12:59 AM
[SOLVED] Sed script within bash script produces unexpecte EOF error Kgeil Linux - Newbie 8 05-16-2019 12:41 AM
Sed replace not working in bash script. TheOnlyQ Programming 2 06-21-2012 12:33 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration