LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-05-2018, 09:02 AM   #1
lewisf152
LQ Newbie
 
Registered: Oct 2018
Posts: 6

Rep: Reputation: Disabled
CentOS 7 cat command truncated by sed command unable to be stored in a variable


Synopsis:

Software to install package dependencies based on distribution

Command used:

Quote:
cat /etc/os-release | sed -n 's/NAME/&/p' | sed '2,$ d' | tr 'NAME=' ' ' | tr '"' " " | tr ' ' ' '

Problem:

When running this command I get a nice truncated output akin to this:
CentOS Linux

Which is exactly what I want it to do... Except it's not.


I want to use this output in an if statement to install packages accordingly. Now I could have just gone brute force and thrown all the install paths at it, but that would be inefficient

Alongside that it would be nice if I could get rid of the spacing at the start, but that's not imperative.

thanks
 
Old 10-05-2018, 09:09 AM   #2
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,440

Rep: Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784
Quote:
Originally Posted by lewisf152 View Post
Synopsis:

Software to install package dependencies based on distribution

Command used:

Code:
cat /etc/os-release | sed -n 's/NAME/&/p' | sed '2,$ d' | tr 'NAME=' ' ' | tr '"' " " | tr ' ' ' '

Problem:

When running this command I get a nice truncated output akin to this:
CentOS Linux

Which is exactly what I want it to do... Except it's not.

I want to use this output in an if statement to install packages accordingly. Now I could have just gone brute force and thrown all the install paths at it, but that would be inefficient

Alongside that it would be nice if I could get rid of the spacing at the start, but that's not imperative.
...
The "cat" command doesn't install software packages. As far as I can see, the "cat" command is doing exactly what it's supposed to do.

If this is a part of a script, you need to post the WHOLE script, not just a tiny part of it. You also need to use CODE tags when doing so, and notice where I've quoted you above I've used CODE tags for your posted command, please do the same in future.
 
Old 10-05-2018, 09:21 AM   #3
lewisf152
LQ Newbie
 
Registered: Oct 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
defining variable with truncated input no work

Apologies, bad wording. Here's the script...

I tried to note up as much as possible.

Code:
#!/bin/bash


## The following command is the command which extrapolates the Linux distro
## cat /etc/os-release | sed -n 's/NAME/&/p' | sed '2,$ d' | tr 'NAME=' ' ' | tr '"' " "
## The problem becomes using this in a variable definition ??????
## If I try to do version="cat /etc/os-release | sed -n 's/NAME/&/p' | sed '2,$ d' | tr 'NAME=' ' ' | tr '"' " ""
## I get weird errors. I tried to change "" to '' and various other weird solutions which doesn't work.

## The only conceivable solution I can think of is writing to a text file, but I want to avoid this as much as possible.

## This will be contained inside of an if command, for CentOS
## There will be additional conditionals used for other distributions.

if [$version='CentOS Linux'];then
        echo 'Checking for whois'
        if [ -x "$(command -v whois)" ];then
                echo 'Whois not installed'
                echo 'Installing who-is'
                y | yum install whois
        fi


        echo 'Checking for dig'
        if [ -x "$(command -v bind-utils)" ];then
                echo 'Dig not installed'
                echo 'Installing dig'
                y | yum install bind-utils
        fi


        if [ -x "$(command -v traceroute)" #;then
                echo "Traceroute facility unfound."
                echo "Installing traceroute"
                y | yum install traceroute
        fi
fi
 
Old 10-05-2018, 09:37 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,104

Rep: Reputation: 1310Reputation: 1310Reputation: 1310Reputation: 1310Reputation: 1310Reputation: 1310Reputation: 1310Reputation: 1310Reputation: 1310Reputation: 1310
Your command doesn't strip the leading spaces of the output, you'll need to do that first before trying to compare it to anything.

Your script as given also includes at least one fatal error and three inefficiencies.

Last edited by TenTenths; 10-05-2018 at 09:39 AM.
 
Old 10-05-2018, 09:53 AM   #5
lewisf152
LQ Newbie
 
Registered: Oct 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
Updated the initial OS locator

Okay, thank you.

I've modified the initial command... What are the fatal errors you mentioned please?

Code:
#!/bin/bash


## This will be contained inside of an if command, for CentOS
## There will be additional conditionals used for other distributions.
## The following command is the command which extrapolates the Linux distro
## cat /etc/os-release | sed -n 's/NAME/&/p' | sed '2,$ d' | tr 'NAME=' ' ' | tr '"' " " | tr '   ' ' ' | sed 's/ //g'
## The problem becomes using this in a variable definition ??????


if [$version='CentOS Linux'];then
        echo 'Checking for whois'
        if [ -x "$(command -v whois)" ];then
                echo 'Whois not installed'
                echo 'Installing who-is'
                y | yum install whois
        fi


        echo 'Checking for dig'
        if [ -x "$(command -v bind-utils)" ];then
                echo 'Dig not installed'
                echo 'Installing dig'
                y | yum install bind-utils
        fi


        if [ -x "$(command -v traceroute)" #;then
                echo "Traceroute facility unfound."
                echo "Installing traceroute"
                y | yum install traceroute
        fi
fi
 
Old 10-05-2018, 09:56 AM   #6
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,104

Rep: Reputation: 1310Reputation: 1310Reputation: 1310Reputation: 1310Reputation: 1310Reputation: 1310Reputation: 1310Reputation: 1310Reputation: 1310Reputation: 1310
Quote:
Originally Posted by lewisf152 View Post
Okay, thank you.

I've modified the initial command... What are the fatal errors you mentioned please?
I'm not your tutor
Try adding set -x at the start of your script and actually run it.
 
Old 10-05-2018, 09:59 AM   #7
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 264

Rep: Reputation: 94
Or try this : https://www.shellcheck.net/
That helps
 
Old 10-05-2018, 09:59 AM   #8
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 264

Rep: Reputation: 94
Please, remove this post.

Last edited by lougavulin; 10-05-2018 at 10:02 AM. Reason: Sorry, duplicate
 
Old 10-05-2018, 10:06 AM   #9
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 3,440

Rep: Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784
I still don't quite understand exactly what it is you're try to achieve here. Why are you writing this script?

I get following when I run it:

Code:
[test@localhost Documents]$ ./test 
./test: line 32: syntax error near unexpected token `fi'
./test: line 32: `        fi'
[test@localhost Documents]$
 
Old 10-05-2018, 10:09 AM   #10
lewisf152
LQ Newbie
 
Registered: Oct 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
Right... okay...

So, the fatal errors was in the IF statement syntax.

As it stands, it wont run as there needs to be a variable called variable which contains the distribution.
I have dry ran everything inside the main if statement and that seems to pull back the results I want.

the -x "$(command -v <command>") selector in the if statement simply checks whether that command exists.
If I was being inefficient, I'd just set it to install them without regard for them already existing on the server; which tbh wouldn't be that inefficient as this is only a prerequisite installer for the main code itself... but I'm really pedantic.

The problem which I'm facing is that I'm unable to set this command:

Code:
cat /etc/os-release | sed -n 's/NAME/&/p' | sed '2,$ d' | tr 'NAME=' ' ' | tr '"' " " | tr '   ' ' ' | sed 's/ //g'
inside of a variable...
 
Old 10-05-2018, 10:48 AM   #11
lewisf152
LQ Newbie
 
Registered: Oct 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
Shellcheck is excellent and thanks for that recommendation.

As a context, the script basically runs a set of basic checks automatically.

It runs a whois check and truncates the output
It digs the nameservers
It pings the IP of the domain
It pings ipv6 of the domain
It runs a traceroute to the domain

The point of the script is to drastically decrease time spent triaging tickets and more time actually fixing them.

I'm sharing the script with other people in the office. Naturally, everyone has their own preferred distribution. I use CentOS. Others use Debian & Ubuntu.

cat, sed and tr are all part of POSIX standard. So that one line command basically just interprets what distribution of Linux the system is running on. I refined that to get me just the distribution name and no fluff.

I then want to interpret the answer of that command as a variable, so that I can use it to determine which package manager to install the appropriate utilities.

whois, dig and traceroute do not come installed on all operating systems.

Each of those three utilities will be contained inside individual IF statements, encompassed by one main IF statement, corresponding to distribution.

For example, I'd have one If statement saying:
Code:
if $distribution = "CentOS"';then

   y  | yum install whois traceroute bind-utils 

fi
And then one for Ubuntu

Code:
if $distribution = "ubuntu"; then
   y | apt-get whois traceroute bind-utils

fi
The problem for me, is that I have abstracted the version's name... but the command I used to do so does not allow me to store it in a variable.

Last edited by lewisf152; 10-05-2018 at 10:50 AM.
 
Old 10-05-2018, 10:55 AM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 20,524

Rep: Reputation: 3589Reputation: 3589Reputation: 3589Reputation: 3589Reputation: 3589Reputation: 3589Reputation: 3589Reputation: 3589Reputation: 3589Reputation: 3589Reputation: 3589
In order to save output of a command as a variable you need to use command substitution.

Code:
version=$(cat /etc/os-release | sed -n 's/NAME/&/p' | sed '2,$ d' | tr 'NAME=' ' ' | tr '"' " " | tr '   ' ' ' | sed 's/ //g')
 
Old 10-05-2018, 11:07 AM   #13
lewisf152
LQ Newbie
 
Registered: Oct 2018
Posts: 6

Original Poster
Rep: Reputation: Disabled
Sweet, thanks =]
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Linux command line cut, awk, grep, sed, cat Questions kirikiriya Linux - General 4 03-11-2018 10:06 AM
SED command failing while substituting variable with error unterminated `s' command vnarvankar Linux - Software 2 03-10-2016 09:10 AM
[SOLVED] Problems using 'cat' command on centOS. teletalky Linux - Newbie 10 07-26-2012 06:46 PM
[SOLVED] Does the output of the previous command get stored in any variable... trist007 Linux - Newbie 8 03-26-2011 11:28 PM
Cat and Sed Command Help. vidyashankara Linux - General 22 06-23-2006 01:32 PM

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

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