Write if condition in shell script and run command based on ubuntu version - shell

Suppose for Ubuntu version 18 or less than that, i want to run command A else command B
How to do that?

You can get version form lsb_release command and then check if major version is less or equal -le to 18. If lsb_release is not avaiable in a system then just cat /etc/os-release and grep from it what is needed.
#!/bin/bash
OS_VER=$(lsb_release -sr | cut -d'.' -f1)
if [[ $OS_VER -le 18 ]]; then
echo "command 1"
else
echo "command 2"
fi

You can get the version of OS from any of these two file:
/etc/lsb_release
/etc/os-release
Then have an if/else command accordingly to check the version.

Related

how do i print out rpm sudo --version in bash script

#!/bin/bash
if rpm -q sudo > /dev/null; then
echo "sudo is installed and version is"
else
echo "sudo is not installed"
fi
hi was wondering if it is possible to print out my sudo version after checking that it is installed. If there's a way, how can I do it
sudo --version output must be parsed and the return status used to determine if it is installed.
First of all, you want the version output to be consistent, regardless of active system locale.
So you invoke it with LANG=C sudo --version.
This prints multiple lines and only the first line last element is the sudo version:
Sudo version 1.9.5p2
Sudoers policy plugin version 1.9.5p2
Sudoers file grammar version 48
Sudoers I/O plugin version 1.9.5p2
Sudoers audit plugin version 1.9.5p2
Using POSIX-shell grammer, lines and field parsing must be done with external tools. Here awk is capable of parsing it all at once:
#!/usr/bin/env sh
# Captures version string parsed with awk:
# so only last entry of first line is captured
if sudo_version=$(LANG=C sudo --version 2>/dev/null | awk 'NR==1{print $NF}')
then printf 'sudo is installed and version is %s\n' "$sudo_version"
else printf 'sudo is not installed\n' >&2
fi
If using Bash, capturing the first line into an array save from using external tools and pipe forking process:
#!/usr/bin/env bash
# Capture version string first line into a Bash array
if read -ra version_array < <(LANG=C sudo --version 2>/dev/null)
then
# Version is the last element of array
printf 'sudo is installed and version is %s\n' "${version_array[-1]}"
else
printf 'sudo is not installed\n' >&2
fi

How to store the output of command "which" in a variable in a bash script?

Environment:
Mac OS Catalina 10.15
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)
Problem:
I have a bash script.
I try to test if MacPOrt is installed.
When I run the command "port -v", it open Macport terminal and the rest of my script is broken.
So I try another approach with the command "which port".
It gives the output "port not found" but I don't succeed to store it in a variable. My variable is empty.
You can see here below the source code of my bash script executing 2 commands:
The first one give an empty value.
The second one give a value if Macport is not installed. But if it is installed, it open the Macport terminal and break my code.
OUTPUT=$(which port 2>&1)
echo "OUTPUT is $OUTPUT"
if echo "$OUTPUT" | grep -q "port not found"; then
echo "MacPort is NOT installed"
else
echo "MacPort is installed"
fi
echo "======================================================================"
OUTPUT=$(port -v 2>&1)
echo "OUTPUT is $OUTPUT"
if echo "$OUTPUT" | grep -q "command not found"; then
echo "MacPort is NOT installed"
else
echo "MacPort is installed"
fi
Here is the output:
OUTPUT is
MacPort is installed
======================================================================
OUTPUT is test.sh: line 11: port: command not found
MacPort is NOT installed
So it seems 'which' command doesn't behave like other commands. How can I store the output of 'which' command?
If it is not possible, I am agree to accept any other trick to test of Macport is installed.
If what you're really looking for is just to check if the port command is available, the right way to do it in bash would be:
if ! command -v port > /dev/null; then
echo "MacPort is not installed"
exit 1
fi
Your script has bugs:
OUTPUT=$(which port 2>&1)
echo "OUTPUT is $OUTPUT"
if echo "$OUTPUT" | grep -q "port not found"; then
echo "MacPort is NOT installed"
else
echo "MacPort is installed"
fi
You are checking for port not command. But which command doesn't actually output anything if port isn't found (whether which outputs anything differs on different systems). This is seen in your output (OUTPUT is empty).
In any case, you can't rely on which as it may not output anything
in some versions and even its return code isn't reliable on some old platforms.
A better approach is to use command or type built-ins of bash (This also avoids calling an external command like which):
if type port &>/dev/null; then
echo "MacPort is installed"
else
echo "MacPort is NOT installed"
fi
if command -v port &>/dev/null; then
echo "MacPort is installed"
else
echo "MacPort is NOT installed"
fi

Bash unexpected token near <<< here document

My terminal's character encoding is set to UTF-8. I went into the terminal program's Profile Preferences / Compatibility and clicked Reset Compatibility Options to Defaults.
I ssh'd to another machine using this terminal window.
I ran vi and typed in the following script in by hand (to avoid any unexpected hidden special characters) on that machine. Then I ran it and got syntax error near unexpected token <<<
#!/bin/bash
cd "$1"
if [ $? -ne 0 ]; then
printf 'cd fail'
exit 1
fi
while read name
do
printf 'item: [%s]\n' "$name"
done <<< "$(stat -t ./* | awk -F' ' '{print $13 " " $1}' | sort -r | awk -F' ' '{print $2}')"
This test machine has /bin/bash ... also /bin/sh is a link to /bin/bash. /bin/bash --version says GNU bash, version 2.05a.0(3)-release (i686-pc-linux-gnu)
I can disconnect from that test machine and in the same terminal window type the same script by hand on my machine and it runs fine. On my machine, /bin/bash --version says GNU bash, version 4.3.42(1)-release (x86_64-redhat-linux-gnu)
What's causing the syntax error on the test machine? Is <<< not supported in that earlier version of Bash?
sure - that feature came in with 2.05b which git shows as July 2002, while 2.05a was November 2001.
bash's developer doesn't put dates in the changelog, but with some patience you can see the changes in git CHANGES

sed command garbled for bash 3.0

I have gone through other threads Sed command garbled didnt work
but it didn't helped me
flag=1
echo "enter the folder into which you want to capture"
read logs
mkdir $logs
path=/user/gur40139/shell/angel
for i in $path/*.tra*
do
value=$( grep -ic \*= $i )
if [ $value -ge $flag ]
then
name=`basename $i .tra\*`
echo -e "count is $value\n" >> $path/$logs/log_"$name".txt
sed -n '/\*=/ {n;p}' $i|sed 2n\;G >> $path/$logs/log_"$name".txt
fi
done
echo -e "\nDone\n"
Error:
sed: command garbled: /\*=/ {n;p}
Additional Note: This code is working properly on bash 4.1 version but I want to test it in 3.0, There many options which are not even working like sed --version.
sed -n '/\*=/ {n;p;}' ...
you need to terminate the line after the p so a ; or a new line. Your code will certainly work on recent GNU sed but not on posix version

Iteration is not working

I am trying to get this script to work. It works fine when I execute it locally but it's not iterating through IP file for remote servers list in the file.
#!/bin/bash
entry=$(cat IPfile)
for i in $entry
do
ssh -q "$entry"
if [[ -n $(egrep "Red Hat Enterprise Linux Server release 5" /etc/redhat-release) ]]; then
sed -i 's/#includedir/##includedir/' /etc/sudoers
fi
done
Are you sure you don't mean ssh -q "$i"? $entry presumably expands to many values.
Edit
I assume you want the grep/sed to occur on each server in the ip list. What your script does is to ssh into each server, then wait for instructions. This should help.
#!/bin/bash
for ip in $(<IPfile); do
# Tell the remote server to start bash, but since its
# standard input is not a TTY it will start bash in
# noninteractive mode.
ssh -q "$ip" bash <<-SSH
if [ ! -r /etc/redhat-release ]; then
printf 'ip "%s" did not have a redhat-release file.\n' "$ip"
elif fgrep -q 'Red Hat Enterprise Linux Server release 5' /etc/redhat-release; then
sed -i 's/#includedir/##includedir/' /etc/sudoers
else
printf 'ip "%s" was not rhel server 5.\n' "$ip"
fi
SSH
done
Some distros don't remove /etc/redhat-release file and make it as part of their own release package. If the script is intended to run strictly on RHEL5, check the version of redhat-release package instead.
The conditional statement will then be:
if version=$(rpm -q --qf "%{version}" redhat-release); then
# it is RHEL
if [ ${version:0:1} == 5 ]; then
# it is RHEL 5
fi
fi
It's because the ssh -q command is "eating" your list. You should add the "-n" option to ssh to prevent it from getting your list from the standard input.

Resources