Using sed with quote marks - bash

I'm try to get sed to replace a line in /etc/lsb-release, I'm using the following code:
x=$( stdbuf -oL /bin/bash \-c '(sudo sed -i "s/DISTRIB_DESCRIPTION=.*/DISTRIB_DESCRIPTION=Linux Lite 2.4/g" /etc/lsb-release && echo "Linux Lite 2.4" | sudo tee /etc/llver && echo "Linux Lite 2.2 LTS \n \l" | sudo tee /etc/issue && sleep 2 )' 2>&1 |
stdbuf -oL sed -n -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p'|
zenity --progress --title="Updating version information..." --pulsate \
--width=600 --auto-close )
I have to use zenity as it is part of a large file. So I want the last line in /etc/lsb-release to show as:
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Linux Lite 2.2"
with the existing code, it displays as:
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION=Linux Lite 2.2
So I would like to preserve the quote marks around "Linux Lite 2.2"
Thank you :)

With
x=$( stdbuf -oL /bin/bash \-c '(sudo sed -i "s/DISTRIB_DESCRIPTION=.*/DISTRIB_DESCRIPTION=\"Linux Lite 2.4\"/g" /etc/lsb-release && echo "Linux Lite 2.4" | sudo tee /etc/llver && echo "Linux Lite 2.2 LTS \n \l" | sudo tee /etc/issue && sleep 2 )' 2>&1 |
stdbuf -oL sed -n -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p'|
zenity --progress --title="Updating version information..." --pulsate \
--width=600 --auto-close )
Relevant bit:
# vv vv
'... "s/DISTRIB_DESCRIPTION=.*/DISTRIB_DESCRIPTION=\"Linux Lite 2.4\"/g" ...'

Related

Multiline heredoc with sudo in Dockerfile

We use our local repo in sources.list, and for 20.04 it required to add in apt.conf.d
Acquire::https::local_repo.local_host.com::Verify-Peer "false";
With Bash it works, as using,
sudo tee -a /etc/apt/apt.conf.d/80ssl-exceptions > /dev/null <<'EOF'
Acquire::https::local_repo.local_host.com::Verify-Peer "false";
EOF
But I don't find a solution to do it for Dockerfile.
I've tried it with different escape character/new line and so on, but always unsuccessful.
For example,
sudo tee -a /etc/apt/apt.conf.d/80ssl-exceptions > /dev/null <<'EOF' \
Acquire::https::local_repo.local_host.com::Verify-Peer "false"; \
EOF
Results - /bin/sh: 1: EOF: not found
To note that cat or echo is not an option, also adding those 3 line in a script is also not preferable.
If you only have one line to append then I wouldn't use a heredoc. It's simpler to use echo:
RUN echo 'Acquire::https::local_repo.local_host.com::Verify-Peer "false";' | \
sudo tee -a /etc/apt/apt.conf.d/80ssl-exceptions > /dev/null
Or cat:
RUN cat <<< 'Acquire::https::local_repo.local_host.com::Verify-Peer "false";' | \
sudo tee -a /etc/apt/apt.conf.d/80ssl-exceptions > /dev/null
Or send the string directly to sudo tee:
RUN sudo tee -a /etc/apt/apt.conf.d/80ssl-exceptions > /dev/null \
<<< 'Acquire::https::local_repo.local_host.com::Verify-Peer "false";'
Note that the latter two options may require you to also set SHELL /bin/bash since <<< is a bash-ism not available in plain sh.

Reliable way of finding out debian release name

I have a script that needs to know what is the release name of a debian system (example: trusty, sid, wheezy etc). I know that I can find out if I am on a Debian based system by looking for /etc/debian_version, but:
cat /etc/debian_version
cat /etc/issue
on Debian Stable produces:
root#07156660e2cd:/# cat /etc/debian_version
7.8
root#07156660e2cd:/# cat /etc/issue
Debian GNU/Linux 7 \n \l
on Ubuntu produces:
```root#a81e3f32b147:/# cat /etc/issue
Ubuntu 14.04.2 LTS \n \l
root#a81e3f32b147:/# cat /etc/debian_version
jessie/sid
```
How I get Ubuntu's codename (in this case 'Trusty')? I don't want to have to maintain a dictionary of release versions to names please. Is there a way in the system to find this information out?
Thanks!
Use lsb_release:
lsb_release -c
#svlasov answer returns a little too much
lsb_release -sc returns just the code name
example
$ lsb_release -sc
utopic
In docker images I can't use uname or another things. So I am using:
DEBIAN_RELEASE=$(awk -F'[" ]' '/VERSION=/{print $3}' /etc/os-release | tr -cd '[[:alnum:]]._-' )
or not depending on /etc/os-release
awk -F'[" ]' '/VERSION=/{print $3}' /etc/*-release | head -1 |tr -cd '[[:alnum:]]._-'
PS:
For docker images based on Debian I am using:
RUN export DEBIAN_FRONTEND=noninteractive && \
export DEBIAN_RELEASE=$(awk -F'[" ]' '/VERSION=/{print $3}' /etc/os-release | tr -cd '[[:alnum:]]._-' ) &&
echo "remove main from /etc/apt/sources.list" && \
sed -i '/main/d' /etc/apt/sources.list && \
echo "remove contrib from /etc/apt/sources.list" && \
sed -i '/contrib/d' /etc/apt/sources.list && \
echo "remove non-free from /etc/apt/sources.list" && \
sed -i '/non-free/d' /etc/apt/sources.list && \
echo "deb http://httpredir.debian.org/debian ${DEBIAN_RELEASE} main contrib non-free" >> /etc/apt/sources.list && \
echo "deb http://httpredir.debian.org/debian ${DEBIAN_RELEASE}-updates main contrib non-free" >> /etc/apt/sources.list && \
echo "deb http://security.debian.org ${DEBIAN_RELEASE}/updates main contrib non-free" >> /etc/apt/sources.list && \
set -x &&\
apt-get update

Piping commands in a shell script

I want to write a script that opens a shell with a few tabs, and i want each tab to execute somthing automaticly. for some reason when i pipe the commands it does not work.
gnome-terminal \
--tab-with-profile=Titleable -t "A" \
--tab-with-profile=Titleable -t "B" -e "sudo tail -f /var/log/syslog" \
--tab-with-profile=Titleable -t "C" -e "sudo tail -f /var/log/syslog | grep txt"
for some reason for Tab A&B work but in C the grep txt is ignored.
Anyone know why?
Thanks
Mat
Use a shell to call your command:
gnome-terminal \
--tab-with-profile=Titleable -t "A" \
--tab-with-profile=Titleable -t "B" -e "sudo tail -f /var/log/syslog" \
--tab-with-profile=Titleable -t "C" -e 'sh -c "sudo tail -f /var/log/syslog | grep txt"'

Bash piping issue

I need to execute the following grep query as an argument for konsole (the kde terminal)
grep -R -i -n -A 2 -B 2 --color=always -R "searchtext" * | less -R
works for the current terminal.
konsole --workdir `pwd` -e grep -R -i -n -A 2 -B 2 --color=always -R "searchtext" * | less -R
works, but the konsole window displays the grep query without less pipe.
Ideally I want konsole to spawn as seperate process with konsole &
and send the grep command with less as an argument for konsole -e
You need to run the pipe in a shell.
konsole --workdir pwd -e bash -c 'grep -R -i -n -A 2 -B 2 --color=always -R "searchtext" * | less -R'

Wget page title

Is it possible to Wget a page's title from the command line?
input:
$ wget http://bit.ly/rQyhG5 <<code>>
output:
If it’s broke, fix it right - Keeping it Real Estate. Home
This script would give you what you need:
wget --quiet -O - http://bit.ly/rQyhG5 \
| sed -n -e 's!.*<title>\(.*\)</title>.*!\1!p'
But there are lots of situations where it breaks, including if there is a <title>...</title> in the body of the page, or if the title is on more than one line.
This might be a little better:
wget --quiet -O - http://bit.ly/rQyhG5 \
| paste -s -d " " \
| sed -e 's!.*<head>\(.*\)</head>.*!\1!' \
| sed -e 's!.*<title>\(.*\)</title>.*!\1!'
but it does not fit your case as your page contains the following head opening:
<head profile="http://gmpg.org/xfn/11">
Again, this might be better:
wget --quiet -O - http://bit.ly/rQyhG5 \
| paste -s -d " " \
| sed -e 's!.*<head[^>]*>\(.*\)</head>.*!\1!' \
| sed -e 's!.*<title>\(.*\)</title>.*!\1!'
but there is still ways to break it, including no head/title in the page.
Again, a better solution might be:
wget --quiet -O - http://bit.ly/rQyhG5 \
| paste -s -d " " \
| sed -n -e 's!.*<head[^>]*>\(.*\)</head>.*!\1!p' \
| sed -n -e 's!.*<title>\(.*\)</title>.*!\1!p'
but I am sure we can find a way to break it. This is why a true xml parser is the right solution, but as your question is tagged shell, the above it the best I can come with.
The paste and the 2 sed can be merged in a single sed, but is less readable. However, this version has the advantage of working on multi-line titles:
wget --quiet -O - http://bit.ly/rQyhG5 \
| sed -n -e 'H;${x;s!.*<head[^>]*>\(.*\)</head>.*!\1!;T;s!.*<title>\(.*\)</title>.*!\1!p}'
Update:
As explain in the comments, the last sed above uses the T command which is a GNU extension. If you do not have a compatible version, you can use:
wget --quiet -O - http://bit.ly/rQyhG5 \
| sed -n -e 'H;${x;s!.*<head[^>]*>\(.*\)</head>.*!\1!;tnext;b;:next;s!.*<title>\(.*\)</title>.*!\1!p}'
Update 2:
As above still not working on Mac, try:
wget --quiet -O - http://bit.ly/rQyhG5 \
| sed -n -e 'H;${x;s!.*<head[^>]*>\(.*\)</head>.*!\1!;tnext};b;:next;s!.*<title>\(.*\)</title>.*!\1!p'
and/or
cat << EOF > script
H
\$x
\$s!.*<head[^>]*>\(.*\)</head>.*!\1!
\$tnext
b
:next
s!.*<title>\(.*\)</title>.*!\1!p
EOF
wget --quiet -O - http://bit.ly/rQyhG5 \
| sed -n -f script
(Note the \ before the $ to avoid variable expansion.)
It seams that the :next does not like to be prefixed by a $, which could be a problem in some sed version.
The following will pull whatever lynx thinks the title of the page is, saving you from all of the regex nonsense. Assuming the page you are retrieving is standards compliant enough for lynx, this should not break.
lynx -dump example.com | sed '2q;d'

Resources