LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-31-2017, 07:20 AM   #16
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895

Its not very pretty but simple.
Code:
ostype=$(lsb_release -d | grep CentOS)
If the string $ostype is empty then the OS is Ubuntu.
 
Old 05-31-2017, 04:11 PM   #17
gkasica
Member
 
Registered: Jan 2011
Location: Jackson WI
Distribution: Fedora Core/Generic
Posts: 116

Original Poster
Rep: Reputation: 0
OK I've come up with the below script and tested on CentOS with the indicated errors...I have NO idea what to do here to fix this my understanding is that is it shou;d be finding at least one "CenntOS" out there and firing the CentOS section...I've obviously commented out the patching and reboot for testing - taking down a server alot is sort of job limiting


#!/bin/bash
#
value=$( grep -ic "CentOS" /etc/*release* )
value1=$( grep -ic "Ubuntu" /etc/*release* )
#
# CentOS
#
if [ $value -ge 1 ]
then
echo "I found CerntOS"
# yum -y update
sleep 3
# reboot
#
# Ubuntu
#
elif [ $value1 -ge 1 ]
then
echo "I found Ubuntu"
#
# apt-get update
sleep 3
# apt-get -y dist-upgrade
sleep 3
# apt-get -y autoremove
sleep 3
# reboot
#
else
echo "Neither CentOS or Ubuntu Found"
#
fi

[root@stadmnwksdb01 ~]# cat ./ossupdate.sh
#!/bin/bash
#
value=$( grep -ic "CentOS" /etc/*release* )
value1=$( grep -ic "Ubuntu" /etc/*release* )
#
# CentOS
#
if [ $value -ge 1 ]
then
echo "I found CerntOS"
# yum -y update
sleep 3
# reboot
#
# Ubuntu
#
elif [ $value1 -ge 1 ]
then
echo "I found Ubuntu"
#
# apt-get update
sleep 3
# apt-get -y dist-upgrade
sleep 3
# apt-get -y autoremove
sleep 3
# reboot
#
else
echo "Neither CentOS or Ubuntu Found"
#
fi

[root@stadmnwksdb01 ~]# ./ossupdate.sh
./ossupdate.sh: line 8: [: too many arguments
./ossupdate.sh: line 17: [: too many arguments
Neither CentOS or Ubuntu Found

[root@stadmnwksdb01 ~]# cat /etc/*release*
CentOS release 6.3 (Final)
cat: /etc/lsb-release.d: Is a directory
CentOS release 6.3 (Final)
CentOS release 6.3 (Final)
cpe:/o:centos:linux:6:GA
 
Old 05-31-2017, 04:20 PM   #18
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Did you see my previous post? The lsb_release command was suggested in post #2 and is available on both systems.

Code:
value=$(lsb_release -d | grep CentOS)
if [[ -z $value ]]; then
  echo "I found Ubuntu"
else
  echo "I found CentOS"
fi

Last edited by michaelk; 05-31-2017 at 04:25 PM.
 
1 members found this post helpful.
Old 05-31-2017, 08:24 PM   #19
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.
 
Old 06-01-2017, 12:25 AM   #20
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I would suggest you to insert set -xv at the beginning of your script to see what's happening.
You will see:
Code:
$ grep -ic "Ubuntu" /etc/*release* 
/etc/lsb-release:2
/etc/os-release:6
this grep did not return a single number, therefore $value is not a number.
Also try www.shellcheck.net to fix other errors in your script.
(and also please use [code]here comes your code[/code] to keep formatting and make it more readable.
 
Old 06-01-2017, 02:14 PM   #21
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
critical commands can be prepended with an echo for testing:
Code:
echo sudo apt-get update

but...
Quote:
Originally Posted by gkasica View Post
now the dirty way to do it just to let it fall through and fail on the systems that don't have the other OS update utilities but I'm trying to not do that.
i think this isn't such a bad solution at all.
with a bit of additional testing before executing the command, i think it's a pretty safe indicator whether yum or apt is installed.
 
Old 06-01-2017, 02:48 PM   #22
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
The OP marked it as solved so I assume the script is working now.

As a FYI it appears your if statement was trying to check the return status of grep but the contents of $value was the actual output. The return status is contained in the variable $?.
 
  


Reply

Tags
shell script, update



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
[SOLVED] Help needed for simple IP reset script Joeblah Linux - Networking 5 07-16-2012 11:27 AM
Help needed with simple bash script clifford227 Linux - Newbie 4 10-03-2011 10:46 AM
simple shell script help needed vbsaltydog Linux - Networking 10 05-02-2006 01:47 AM
simple script needed drum2jc Linux - Software 1 01-05-2006 12:28 AM
simple backup script needed ferret_dude Programming 4 05-24-2004 08:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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