LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-17-2020, 10:14 PM   #1
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Rep: Reputation: Disabled
Bash Help


All,

Have this script to pull version from the system:

Code:
for line in $lines; do
    if [[ "$line" =~ "VERSION_ID=" ]]; then
        ver=${line:11:5}
        echo "$ver"
    fi
done < cat "/etc/os-release"
But it produces this error string:
Quote:
syntax error near unexpected token `"/etc/os-release"'
done < cat "/etc/os-release"'
By all the HOWTOs this is suppose to work, but since it says "syntax" gotta be something simple, right?

if it is obvious to you, please help!

Cheers!

TBNK
 
Old 06-17-2020, 11:38 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
After the <, a filename is expected. In your case, that filename is cat. Then another string appears, "/etc/os-release". That's the error.

This is a new type of cat abuse. Leave out the cat, and the syntax is correct. However, since the script doesn't read from standard input, it is pointless to redirect it. I believe you want to replace the for loop with something like
Code:
while read line
do if ...
...
done < /etc/os-release

Last edited by berndbausch; 06-17-2020 at 11:40 PM.
 
1 members found this post helpful.
Old 06-18-2020, 02:07 AM   #3
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
I know this doesn't answer your question directly re the script, but to get what you want you could do :-

grep VERSION_ID /etc/os-release | cut -f2,2 -d"="
 
1 members found this post helpful.
Old 06-18-2020, 02:57 AM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Since the file defines shell compatible variables, how about

Code:
$ source /etc/os-release
$ echo "$VERSION_ID"
This way you can also conveniently get all the other values without complicated string manipulations.
 
1 members found this post helpful.
Old 06-18-2020, 03:36 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Yes, you got several good solutions. Additionally you could [try to] fix the syntax error by:
Code:
for line in $lines; do
...
done <<< $(cat "/etc/os-release")
although it is not a solution, because the for loop does not read the result of cat.
 
Old 06-18-2020, 10:36 AM   #6
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Original Poster
Rep: Reputation: Disabled
Fixed

All,

OK I worked on this, before I got to read your inputs and new complete code is:

Code:
while read line; do
    if [[ "$line" =~ "VERSION_ID=" ]]; then
        ver=${line:12:5}
        echo "V=> $ver"
    fi
done < /etc/os-release
case $ver in
    12.04) vername="Precise"
    ;;
    14.04) vername="Trusty"
    ;;
    16.04) vername="Xenial"
    ;;
    18.04) vername="Bionic"
    ;;
    20.04) vername="Focal"
    ;;
esac
echo "VN=> $vername"
If I have the version names right I can then use this to update my "/etc/apt/sources.list" file by version for all the apps I download, install and use.

Hope that explains what I'm doing here! I see some of you understood what I'm trying to do!

Cheers!

TBNK
 
Old 06-18-2020, 10:51 AM   #7
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
/etc/os-release includes VERSION_CODENAME as well.
Code:
. /etc/os-release
echo ${VERSION_CODENAME^}

Last edited by shruggy; 06-18-2020 at 12:22 PM.
 
Old 06-18-2020, 11:24 AM   #8
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
Code:
cat /etc/os-release | grep -i version_codename | sed 's/VERSION\_CODENAME\=//'
Although reinventing wheels can be fun sometimes.
 
Old 06-18-2020, 11:43 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by Shadow_7 View Post
Code:
cat /etc/os-release | grep -i version_codename | sed 's/VERSION\_CODENAME\=//'
Although reinventing wheels can be fun sometimes.
this is deprecated, do not use three commands (and two pipes) if a simple oneliner is enough.
You can solve it with grep/sed/awk/perl/python/whatever...
 
Old 06-19-2020, 03:01 AM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by TBotNik View Post
Fixed
Please mark your thread SOLVED (see my signature).
Seriously, try to give at least that much back to the community.
 
Old 07-02-2020, 08:39 AM   #11
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
/etc/os-release includes VERSION_CODENAME as well.
Code:
. /etc/os-release
echo ${VERSION_CODENAME^}
shruggy,

I like thin cause can just put another "IF" in my while loop and then emliminate the "CAse" altogether!

Thanks!

TBNK
 
Old 07-02-2020, 08:46 AM   #12
TBotNik
Member
 
Registered: May 2016
Location: Greenville, TX
Distribution: Kubuntu 18.04
Posts: 796

Original Poster
Rep: Reputation: Disabled
Repo Update Script

All,

Thanks to all your help, here is my final repository update script:

Code:
#! /bin/bash
# Script to install the needed repositories
# Run with SUDO access/rights

# copy replace the repository "sources.list" file
cp -f /etc/apt/sources.list "/etc/apt/sources.list.bup";

# Get version and version name to make sure you have 
# the right version repositories
source /etc/os-release
ver="$VERSION_ID"
vername="$VERSION_CODENAME"
# echo "V=> $ver"
# echo "VN=> $vername"
ruser=$SUDO_USER;

# Add missing repositories
echo "Adding Repositories!";
add-apt-repository -y ppa:videolan/stable-daily;
add-apt-repository -y ppa:otto-kesselgulasch/gtmp;
add-apt-repository -y ppa:gnome3-team/gnome3;
add-apt-repository -y ppa:webupd8team/java;
add-apt-repository -y ppa:webupd8team/y-ppa-manager;
add-apt-repository -y ppa:webupd8team/rythmbox;
add-apt-repository -y ppa:tualatrix/pps;
add-apt-repository -y ppa:transmissionbt/ppa;
add-apt-repository -y ppa:ubuntu-wines/ppa;
add-apt-repository -y ppa:kubuntu-ppa/backports;
add-apt-repository -y ppa:pitti/postgresql;
add-apt-repository -y ppa:jacob/media;
add-apt-repository -y ppa:oliver-berten/misc;
add-apt-repository -y ppa:samoilov-lex/sway;

# Set repo keys
echo "Adding Repository Validation Keys!";
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 0624A220;
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1378B444;
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 2D75E850;
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 2D9A3C5B;
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B1510FD;
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6E80C6B7;
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 75198A89;
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 8AC93F7A;
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys A1F196A8;
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys AF1CDFA9;
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys BB901940;
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys CA186228;
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EEB23232;
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F9CB8DB0;
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys FC91AE7E;
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 614C4B38;

# Set repo keys for 3rd party repos
apt-get -y -q install faenza-icon-theme equinox-theme    
wget -q -O- http://download.opensuse.org/repositories/Java:/esmska/common-deb/Release.key | apt-key add -
wget http://www.fbreader.org/desktop/debian/geometer.fbreader.org.asc -O- | apt-key add -
wget -q -O- http://archive.getdeb.net/getdeb-archive.key | apt-key add -
apt-get --quiet update
apt-get -y -q --allow-unauthenticated install medibuntu-keyring && apt-get --quiet update
wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | apt-key add -
wget http://www.webmin.com/jcameron-key.asc -O- | apt-key add -
wget http://drbl.sourceforge.net/GPG-KEY-DRBL && apt-key add GPG-KEY-DRBL

# Update/Upgrade the system
apt -q -y update && apt -q -y upgrade
Some of you will probably see a few more I should add, but with version and versionname assigned I now get all the right version appropriate repositories. This could be also modified to run against the original /etc/apt/sources.list to make sure it is calling all ther right version repos, though by default it should.

Cheers!

TBNK

PS
Ok marking this "Solved" but you can still post other repos you like, but please explain why and what programs/packages they enable.

TBNK
 
  


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
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 05:52 AM
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
Help, help help help help drknownothing Linux - Networking 2 10-24-2004 03:05 AM
help, help, help, help, help....cmi8738 will not work... AMArt79196 Red Hat 2 07-05-2004 06:13 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:02 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