LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-24-2017, 01:58 PM   #1
agent_mach
LQ Newbie
 
Registered: Dec 2010
Posts: 11

Rep: Reputation: 0
Problem with shell script


Hello Gurus/Experts,

I started working on shell scripting recently, and drafted a attached shell script which can be run on both RHEL versions i.e. 6.x and 7.x.

But while running this script i am getting below errors:

Code:
[root@rhel74 ~]# ./Demo-PreReq-RHEL.sh
./Demo-PreReq-RHEL.sh: line 10: [: missing `]'
./Demo-PreReq-RHEL.sh: line 24: [: too many arguments
./Demo-PreReq-RHEL.sh: line 34: [: missing `]'
Redirecting to /bin/systemctl restart sshd.service
./Demo-PreReq-RHEL.sh: line 54: [: missing `]'
Redirecting to /bin/systemctl stop firewalld.service
./Demo-PreReq-RHEL.sh: line 84: [: too many arguments
./Demo-PreReq-RHEL.sh: line 113: [: missing `]'
24 Nov 14:32:23 ntpdate[24612]: the NTP socket is in use, exiting
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 xx.xx.xx.xx .INIT.          12 u    -  51    0    0.000    0.000   0.000
 xx.xx.xx.xx   .INIT.          12 u    -  51    0    0.000    0.000   0.000
./Demo-PreReq-RHEL.sh: line 180: [: missing `]'

It would be really great if someone can point me out what exactly i am missing here.

Script is copied in the attached txt file.

Thanks in advance.
Attached Files
File Type: txt Demo-PreReq-RHEL.txt (4.7 KB, 21 views)
 
Old 11-24-2017, 02:42 PM   #2
GazL
LQ Veteran
 
Registered: May 2008
Distribution: CRUX 3.7
Posts: 6,612

Rep: Reputation: 4721Reputation: 4721Reputation: 4721Reputation: 4721Reputation: 4721Reputation: 4721Reputation: 4721Reputation: 4721Reputation: 4721Reputation: 4721Reputation: 4721
You need a space before the closing ']'.
 
Old 11-24-2017, 10:46 PM   #3
giis
Member
 
Registered: Nov 2013
Location: Third Rock from Moon
Distribution: RPM/DEB based and LFS
Posts: 73

Rep: Reputation: Disabled
On few places you are using correct spacing like
Quote:
if [ $get_DNS = $DNS ]
and others are wrong
Quote:
if [ ! -f /etc/redhat-release]
As other member mentioned you need to add missing space before ]
 
Old 11-25-2017, 02:23 AM   #4
agent_mach
LQ Newbie
 
Registered: Dec 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks GazL/giis for pointing out the problem. I corrected that and now i am not seeing "./Demo-PreReq-RHEL.sh: line 10: [: missing `]'" issue.

But i am still seeing below error:

./Demo-PreReq-RHEL.sh: line 24: [: too many arguments
./Demo-PreReq-RHEL.sh: line 84: [: too many arguments

is there any issue with below block of code :

Code:
# Checking RHEL versions

rhel6="cat /etc/redhat-release | grep -o 6"
rhel7="cat /etc/redhat-release | grep -o 7"
OS_VER=""

if [ $rhel6 = 6 ]         ==> 24th line
	then
	OS_VER=6
else
	OS_VER=7
fi
And

Code:
# Enable NTP to sync time from "ntp.demo.net"

DNS="10.100.105.11"
get_DNS="cat /etc/resolv.conf | grep -o 10.100.105.11"

if [ $get_DNS = $DNS ]          ==> 84th line
	then
	echo "DNS $DNS is configured."	
else
	echo "nameserver 10.100.105.11" >> /etc/resolv.conf
fi
Thanks.

Last edited by agent_mach; 11-25-2017 at 02:26 AM.
 
Old 11-25-2017, 03:35 AM   #5
agent_mach
LQ Newbie
 
Registered: Dec 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Anyways i have figured out the error i made ... it should be like this

rhel6=`cat /etc/redhat-release | grep -o 6`
rhel7=`cat /etc/redhat-release | grep -o 7`


Thanks a lot for the help guys.

-Ashish
 
Old 11-25-2017, 04:29 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 19,919

Rep: Reputation: 6743Reputation: 6743Reputation: 6743Reputation: 6743Reputation: 6743Reputation: 6743Reputation: 6743Reputation: 6743Reputation: 6743Reputation: 6743Reputation: 6743
this is a bit overcomplicated:
Code:
#instead of:
cat file | grep pattern
#just use
grep pattern file
Next, you can try www.shellcheck.net to check your script. This will also help you to write better code.
you can do something like this too:
Code:
grep -q 6 /etc/redhat-release && OS_VER=6
 
  


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
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
shell script problem bondoq Linux - General 3 07-03-2006 06:33 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:20 AM.

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