Sponsored Content
Operating Systems Linux Debian Using sed with bash variables Post 302945872 by linuxjunkie on Thursday 4th of June 2015 05:15:33 AM
Old 06-04-2015
Question Using sed with bash variables

Hi Guys

I have another problem I'm trying to solve and hope that some one can help me here.

This is the scenario:

I have a file and I want to add a line on the 3rd line of the file using a bash script. but instead its adding the the bash variable $WEBSITE.

Below is the bash script I'm using.
Code:
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11

echo "what is the website ?"
read WEBSITE
if grep -q $WEBSITE "/etc/squid3/acls/allowed-sites"
then
        echo "$WEBSITE already exists"
else
        echo "adding website $WEBSITE"
        sed -ie '3i\$WEBSITE\' /etc/squid3/acls/allowed-sites
        echo "restarting squid"
fi

I have tried a few different ways of doing this with sed but nothing works.

Please help.



Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes them far easier to read and preserves multiple spaces for indenting or fixed width data

Last edited by rbatte1; 06-04-2015 at 06:27 AM.. Reason: CODE tags and spelling/grammar corrections
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

using sed on bash variables (or maybe awk?)

Hi all- I've been fooling with this for a few days, but I'm rather new at this... I have a bash variable containing a long string of various characters, for instance: JUNK=this that the other xyz 1234 56 789 I don't know what "xyz" actually is, but I know that: START=he other and ... (2 Replies)
Discussion started by: rev66
2 Replies

2. Shell Programming and Scripting

Bash variables

Ummm can anybody help me with this one? Its prob quite simple. I bascially have a file name say J1x2x3x7.dat Im using the file name as a variable in a bash script. Want I want to do is extract most of the file name and make it a new variable expect with say one of the number now a... (2 Replies)
Discussion started by: RichieFondel
2 Replies

3. Shell Programming and Scripting

how to use in bash variables and quotes

I have some troubles with variables and quotes... I want: if $URL is empty (no user input) go to http://www.localhost/index.php/ else add this string (search) "?s=+$URL" EXAMPLE: No user input string= http://www.localhost/index.php/ User input = "unix" string=... (3 Replies)
Discussion started by: aspire
3 Replies

4. UNIX for Dummies Questions & Answers

Expanding variables with Ed Bash 3.2.33

Hi, The following code finds the line containing fruits in test.txt and replaces instances of apple with banana. ed -s test.txt <<< $'/fruits/s/apple/banana/g\nw' What I want to do is put variables in the place of fruits, apple and banana. I have tried replacing ' with " to get... (2 Replies)
Discussion started by: de_la_espada
2 Replies

5. Shell Programming and Scripting

Complex bash/sed, variables and nested quotes

Ok, this one isn't for everybody, it's pretty tough and I've spent a good deal of time on it without figuring it out yet. Can anybody get this script to work: #!/bin/bash cq_fname="%let outputfile="/user/cq_"$1".csv";" sed "29s/.*/\"$cq_fname\"/" file1.sas >... (3 Replies)
Discussion started by: nocloud
3 Replies

6. Shell Programming and Scripting

Indirect variables in Bash

Hello, I've spent hours this morning reading various past forum posts and documentation pages but I can't find exactly what I need. I'm trying to call a variable with a variable in the name without having to make a third variable. For example: path=AB legAB=50 leg$path I want to... (8 Replies)
Discussion started by: DFr0st
8 Replies

7. Shell Programming and Scripting

BASH arrays and variables of variables in C++

Sometimes it is handy to protect long scripts in C++. The following syntax works fine for simple commands: #define SHELLSCRIPT1 "\ #/bin/bash \n\ echo \"hello\" \n\ " int main () { cout <<system(SHELLSCRIPT1); return 0; } Unfortunately for there are problems for: 1d arrays:... (10 Replies)
Discussion started by: frad
10 Replies

8. Shell Programming and Scripting

Expect - bash and variables

I was wondering if anyone could provide some assistance. I trying to run an expect script within bash and get the results of a variable called RESULT. I Have tried a few things but none of them have worked. I know that the child process (the expect script) in this instance cannot set a variable... (6 Replies)
Discussion started by: ylafont
6 Replies

9. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies
SYSTEMD.ENVIRONMENT-GENERATOR(7)			   systemd.environment-generator			  SYSTEMD.ENVIRONMENT-GENERATOR(7)

NAME
systemd.environment-generator - systemd environment file generators SYNOPSIS
/lib/systemd/system-environment-generators/some-generator /usr/lib/systemd/user-environment-generators/some-generator /run/systemd/system-environment-generators/* /etc/systemd/system-environment-generators/* /usr/local/lib/systemd/system-environment-generators/* /lib/systemd/system-environment-generators/* /run/systemd/user-environment-generators/* /etc/systemd/user-environment-generators/* /usr/local/lib/systemd/user-environment-generators/* /usr/lib/systemd/user-environment-generators/* DESCRIPTION
Generators are small executables that live in /lib/systemd/system-environment-generators/ and other directories listed above. systemd(1) will execute those binaries very early at the startup of each manager and at configuration reload time, before running the generators described in systemd.generator(7) and before starting any units. Environment generators can override the environment that the manager exports to services and other processes. Generators are loaded from a set of paths determined during compilation, as listed above. System and user environment generators are loaded from directories with names ending in system-environment-generators/ and user-environment-generators/, respectively. Generators found in directories listed earlier override the ones with the same name in directories lower in the list. A symlink to /dev/null or an empty file can be used to mask a generator, thereby preventing it from running. Please note that the order of the two directories with the highest priority is reversed with respect to the unit load path, and generators in /run overwrite those in /etc. After installing new generators or updating the configuration, systemctl daemon-reload may be executed. This will re-run all generators, updating environment configuration. It will be used for any services that are started subsequently. Environment file generators are executed similarly to unit file generators described in systemd.generator(7), with the following differences: o Generators are executed sequentially in the alphanumerical order of the final component of their name. The output of each generator output is immediately parsed and used to update the environment for generators that run after that. Thus, later generators can use and/or modify the output of earlier generators. o Generators are run by every manager instance, their output can be different for each user. It is recommended to use numerical prefixes for generator names to simplify ordering. EXAMPLES
Example 1. A simple generator that extends an environment variable if a directory exists in the file system # 50-xdg-data-dirs.sh #!/bin/bash # set the default value XDG_DATA_DIRS="${XDG_DATA_DIRS:-/usr/local/share/:/usr/share}" # add a directory if it exists if [[ -d /opt/foo/share ]]; then XDG_DATA_DIRS=/opt/foo/share:${XDG_DATA_DIRS} fi # write our output echo XDG_DATA_DIRS=$XDG_DATA_DIRS Example 2. A more complicated generator which reads existing configuration and mutates one variable # 90-rearrange-path.py #!/usr/bin/env python3 """ Proof-of-concept systemd environment generator that makes sure that bin dirs are always after matching sbin dirs in the path. (Changes /sbin:/bin:/foo/bar to /bin:/sbin:/foo/bar.) This generator shows how to override the configuration possibly created by earlier generators. It would be easier to write in bash, but let's have it in Python just to prove that we can, and to serve as a template for more interesting generators. """ import os import pathlib def rearrange_bin_sbin(path): """Make sure any pair of .../bin, .../sbin directories is in this order >>> rearrange_bin_sbin('/bin:/sbin:/usr/sbin:/usr/bin') '/bin:/sbin:/usr/bin:/usr/sbin' """ items = [pathlib.Path(p) for p in path.split(':')] for i in range(len(items)): if 'sbin' in items[i].parts: ind = items[i].parts.index('sbin') bin = pathlib.Path(*items[i].parts[:ind], 'bin', *items[i].parts[ind+1:]) if bin in items[i+1:]: j = i + 1 + items[i+1:].index(bin) items[i], items[j] = items[j], items[i] return ':'.join(p.as_posix() for p in items) if __name__ == '__main__': path = os.environ['PATH'] # This should be always set. # If it's not, we'll just crash, we is OK too. new = rearrange_bin_sbin(path) if new != path: print('PATH={}'.format(new)) Example 3. Debugging a generator SYSTEMD_LOG_LEVEL=debug VAR_A=something VAR_B="something else" /lib/systemd/system-environment-generators/path-to-generator SEE ALSO
systemd-environment-d-generator(8), systemd.generator(7), systemd(1), systemctl(1) systemd 237 SYSTEMD.ENVIRONMENT-GENERATOR(7)
All times are GMT -4. The time now is 07:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy