Sponsored Content
Top Forums Shell Programming and Scripting Executing sed command inside a bash script Post 303027328 by Corona688 on Thursday 13th of December 2018 11:38:18 AM
Old 12-13-2018
quotes-inside-quotes is usually a bad idea. The shell doesn't handle them - or any other shell syntax - in variables as a security measure. You can force it to do so, but that often ends up evaluating a lot of things you don't want it to.

There's probably a way to do this without resorting to quotes-in-quotes. What, exactly, are you trying to accomplish? Don't say "quotes inside quotes", don't say "passing arguments", etc, etc. What is your ultimate goal?

By my best guess, I'd do something like this:

Code:
#!/bin/bash

while [ "$#" -gt 0 ]
do
case "$1" in
-*)    # Collect all -i, -r, etc arguments
        SEDARGS="$SEDARGS $1"
        shift
        ;;
*)    break ;;
esac
done

REGEX="$1"

if [ -z "$SEDARGS" ]
then
        echo sed "$REGEX" "$FILE"
else
        echo sed $SEDARGS "$REGEX" "$FILE"
fi

Run it like:

Code:
./myscript.sh -r -i "regex"

No need for quotes-in-quotes as it takes the arguments as given.

Last edited by Corona688; 12-13-2018 at 12:44 PM..
These 2 Users Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed inside bash script - accessing multiple files

I have a file (email) containing email addresses. I have a second file (terms) that contains simple regular expressions and words/characters. Here are some examples: \.trainee \.group \.web I want to go through email and delete lines containing the expressions/words from terms and write... (1 Reply)
Discussion started by: manouche
1 Replies

2. UNIX for Dummies Questions & Answers

Problem with executing command inside a cron job

Hi All, I have scheduled a script in cron which writes output to the below file. ....>> /data/Target/wrapper_invoke_ds_job_`date '+%Y%m%d'`.ksh_out 2>&1 But the date command is not getting resolved in the format specified. It just resolves to the following. wrapper_invoke_MQ_ds_job_Tue... (3 Replies)
Discussion started by: pkm_oec
3 Replies

3. Shell Programming and Scripting

Bash: executing a command based on organized output

Just learning scripting. I need to remove duplicate managed printers using lpadmin. I have the following script (it's rough and probably a better way to do it) that returns the values as IP (column 1) Printer Name (column 2).command: lpstat -v | grep -E ... (6 Replies)
Discussion started by: Zookpr
6 Replies

4. Programming

Executing a awk command inside perl script --

Hello experts I want to execute a awk command, which reads from txt files and sums the numbers from the first column for those listed only inside a <init> block -- The awk command is like awk '/<\/?init>/{x = !x}x{a++}x && a > 2{sum+=$1}END{printf"%E" "\n", sum} So, I want to execute... (2 Replies)
Discussion started by: Alkass
2 Replies

5. Shell Programming and Scripting

Bash- Command run from script does not pass full parameters with spaces inside

There's a JavaScript file that I call from command line (there's a framework) like so: ./RunDiag.js param1:'string one here' param2:'string two here' I have a shell script where I invoke the above command. I can run it in a script as simple as this #!/bin/bash stuff="./RunDiag.js... (4 Replies)
Discussion started by: AcerAspirant
4 Replies

6. Shell Programming and Scripting

Bash - command is executing but do nothing without complaining

Hello. This part of script do nothing but no complain. Here the directory content : The script run by root cd /root CMD="rmdir -p -v --ignore-fail-on-non-empty .ssh" echo $CMD $CMD The ouput linux:~/bin # ./test_05 rmdir -p -v --ignore-fail-on-non-empty .ssh rmdir: removing directory,... (3 Replies)
Discussion started by: jcdole
3 Replies

7. UNIX for Dummies Questions & Answers

Write pid and command name to a txt file while executing a bash script

Hi All, Just have a requirement, I am executing a bash shell script, my requirement is to catch the pid and job name to a txt file in the same directory, is there anyway to do it? please help me out. Regards Rahul ---------- Post updated at 08:42 AM ---------- Previous update was at... (2 Replies)
Discussion started by: rahulkalra9
2 Replies

8. Shell Programming and Scripting

sed command not working inside ksh script but works fine outside

Hi, I am a bit confused ,why would a sed command work fine outside of ksh script but not inside. e.g I want to replace all the characters which end with a value and have space at end of it. so my command for it is : sed -i "s/$SEPARATOR /$SEPARATOR/g" file_name This is working fine in... (8 Replies)
Discussion started by: vital_parsley
8 Replies

9. Shell Programming and Scripting

Run bash command inside zsh script

Hi, I would like to run following code in bash inside a zsh script. (In this case is output unfortunately very different if you run it in zsh). I tried to put "bash" in front of the code but I obtained following error message "bash: do: No such file or directory " eve though I merged the whole... (7 Replies)
Discussion started by: kamcamonty
7 Replies

10. Shell Programming and Scripting

Need bash script to use a sed command as a variable

I need to be able to use a sed command as a variable in a bash script. I have the sed command that almost works the way I want it. the command is sed -n '/inet/,/}/p' config.boot This gets me this result: inet 192.168.1.245 } I need to get the IP address into a variable so I... (9 Replies)
Discussion started by: edlentz
9 Replies
Encode::Alias(3pm)					 Perl Programmers Reference Guide					Encode::Alias(3pm)

NAME
Encode::Alias - alias definitions to encodings SYNOPSIS
use Encode; use Encode::Alias; define_alias( "newName" => ENCODING); define_alias( qr/.../ => ENCODING); define_alias( sub { return ENCODING if ...; } ); DESCRIPTION
Allows newName to be used as an alias for ENCODING. ENCODING may be either the name of an encoding or an encoding object (as described in Encode). Currently the first argument to define_alias() can be specified in the following ways: As a simple string. As a qr// compiled regular expression, e.g.: define_alias( qr/^iso8859-(d+)$/i => '"iso-8859-$1"' ); In this case, if ENCODING is not a reference, it is "eval"-ed in order to allow $1 etc. to be substituted. The example is one way to alias names as used in X11 fonts to the MIME names for the iso-8859-* family. Note the double quotes inside the single quotes. (or, you don't have to do this yourself because this example is predefined) If you are using a regex here, you have to use the quotes as shown or it won't work. Also note that regex handling is tricky even for the experienced. Use this feature with caution. As a code reference, e.g.: define_alias( sub {shift =~ /^iso8859-(d+)$/i ? "iso-8859-$1" : undef } ); The same effect as the example above in a different way. The coderef takes the alias name as an argument and returns a canonical name on success or undef if not. Note the second argument is ignored if provided. Use this with even more caution than the regex version. Changes in code reference aliasing As of Encode 1.87, the older form define_alias( sub { return /^iso8859-(d+)$/i ? "iso-8859-$1" : undef } ); no longer works. Encode up to 1.86 internally used "local $_" to implement ths older form. But consider the code below; use Encode; $_ = "eeeee" ; while (/(e)/g) { my $utf = decode('aliased-encoding-name', $1); print "position:",pos," "; } Prior to Encode 1.86 this fails because of "local $_". Alias overloading You can override predefined aliases by simply applying define_alias(). The new alias is always evaluated first, and when necessary, define_alias() flushes the internal cache to make the new definition available. # redirect SHIFT_JIS to MS/IBM Code Page 932, which is a # superset of SHIFT_JIS define_alias( qr/shift.*jis$/i => '"cp932"' ); define_alias( qr/sjis$/i => '"cp932"' ); If you want to zap all predefined aliases, you can use Encode::Alias->undef_aliases; to do so. And Encode::Alias->init_aliases; gets the factory settings back. Note that define_alias() will not be able to override the canonical name of encodings. Encodings are first looked up by canonical name before potential aliases are tried. SEE ALSO
Encode, Encode::Supported perl v5.18.2 2014-01-06 Encode::Alias(3pm)
All times are GMT -4. The time now is 12:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy