ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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.
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.
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
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.