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

#!/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

Related

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

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.

Is there a way for me to get specific words from an output of a command in bash?

For example, when running the dpkg command,
dpkg -s autofs
I would get an output like
dpkg-query: package 'autofs' is not installed and no information is available
But I just want to get the
not installed
part so that I can further use it for my script. Is there a command that can help me with it?
First of all, the words not installed are locale specific, so it will fail with anything but English locales. It is also non-predictable because it is not a published API Application Programming Interface.
So, even with some precautions, do not use this:
LC_MESSAGES=C dpkg-query --status autofs 2>&1 | grep -o 'not installed'
Check the return status of the dpkg-query command instead:
#!/usr/bin/env sh
package='autofs'
if dpkg-query --status "$package" >/dev/null 2>&1; then
printf 'Package %s is installed!\n' "$package"
else
printf 'Package %s is not installed!\n' "$package"
fi

grep -v -f of empty file different between script and command line on OS X

In bash, in Terminal on my Mac, (but not in Linux), grep -v -f behaves differently depending on whether it's executed at the command line or in a script. From the command line:
$ touch empty-file #create an empty file
$ printf 'foo' | grep -v -f empty-file
foo
That's as expected. But when that line is in a script, it outputs nothing. Here's the script:
$ cat grep-v-in-script.sh
#!/usr/bin/env bash
printf 'foo\n' | grep -v -f empty-file
printf 'end of script\n'
When I execute that script:
$ ./grep-v-in-script.sh
end of script
If I run that same script in Linux it works as expected:
herdrick#some-linux-server:~$ ./grep-v-in-script.sh
foo
end of script
FWIW on my Mac if I change the 'grep -v -f' to 'grep -f', then it again outputs nothing, but this time that is expected.
Here's my bash version:
$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
Copyright (C) 2007 Free Software Foundation, Inc.
The issue is simply an incompatibility between GNU grep and BSD grep. See the comment on the post by #that-other-guy.
My confusion was due to my having an alias set to use GNU grep. There is, otherwise, no difference between doing this at the command line and in a script.

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

not found error when insert #!/bin/sh

I started learning Linux shell scripting, when I was writing this script I got an error,
./my_script: 4: read: Illegal option -n
./my_script: 5: ./my_script: [[: not found
I found out its because #!/bin/sh line, i can still run the script without that line but it won't execute codes such as /n
#!/bin/sh
# Shell installer for gurb customizer. by Naveen Gamage.
OS=$(lsb_release -si)
ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/')
VER=$(lsb_release -sr)
grabninstall() {
sudo add-apt-repository ppa:danielrichter2007/grub-customizer
sudo apt-get update
sudo apt-get install grub-customizer
}
echo "Installer for GRUB CUSTOMIZER\n"
echo "GURB CUSTOMIZER"
echo "A tool for editing and configuring boot menu (GRUB2/BURG).\n"
read -p "Do you want to install Grub Customizer for $OS ${VER} [$ARCH] ? (Y/n) " -n 1
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "The installer is downloading and installing GRUB Customizer!";
echo "This action may require your password.\n";
grabninstall
else
echo "user quit"
echo "Installation was unsuccessful."
fi
I'm doing this on Ubuntu 12.10.
and which sh gives this output
/bin/sh
any idea where i did wrong?
The problem is that you are using /bin/sh to run the script and on your system /bin/sh -> dash. This means that dash is executing your script. The dash shell does not support [[, but bash does. So you should change the first line in your script (called the Shebang) from #!/bin/sh to #!/bin/bash.
Alternatively, don't use [[ in your script. Only use features support by dash.
Also see this Ubuntu page on what constructs are not supported in dash.

Resources