How to debug this distro dector in bash? - bash

I am trying to make a distribution checker for an installer but am getting errors that I can not figure out. Would someone help with this distribution checker - am I doing the variable declaration wrong here? The script gives me the errors:
read: `NAME=Fedora': not a valid identifier and blank.
Is there any other way I could achieve the same thing without uname? I'll check uname after I have fixed this so that I can have the installer work on mac as well.
#!/usr/bin/bash
#distribution detection system
NAME=$(head -n 1; grep NAME= "/etc/os-release")
#installer promt
read -p "Install tools [y/n]?" insatll_base
case "$insatll_base" in
y|Y ) echo "installing addtional programs and tools";
#checks for fedora
read $NAME;
if [$NAME="Fedora"]; then
dnf install cpan -y;
cpan install Menu::Item;
fi;
read $NAME;
if [$NAME="NAME=Redhat"]; then
dnf install cpan -y;
cpan install Menu::Item;
fi;

Some observations:
There's no need to use head or grep for /etc/os-release. Just put a . before it, and all its variables are loaded.
When two then ... fi blocks contain the same code, there's no need for two if statements.
Myrddin Emrys' answer to How do I prompt for Yes/No/Cancel input in a Linux shell script? provides a good minimal example of how to use case.
Here is one way to do it:
#!/usr/bin/bash
#distribution detection system
if [ -s /etc/os-release ] ; then
. /etc/os-release
#installer prompt
read -p "Install tools [y/n]?" yn
if [ "${yn,,}" = y ] ; then
echo "Installing additional programs and tools...";
#checks for Fedora or Redhat
case "$NAME" in
Fedora|Redhat) dnf install cpan -y
cpan install Menu::Item ;;
esac
fi
fi

Related

Is it possible to add prompt based on keywords passed to bash?

Say we are executing some CLI in bash (not to bash script). Example -
apt-get remove nginx
So is it possible to add a prompt to it like based on keywords ("apt-get remove", etc.)-
You are on a production machine. Are you sure you want to continue [Y/n]?
You can always write a little wrapper script which is located in a directory which is searched first.
#!/usr/bin/env bash
if [ $HOSNTAME = "XYZ" ]; then
while :; do
read -p "This is a production machine. Do you wish to continue [y/n]? " yn
case "$yn" in [Yy]*) break ;; [Nn]*) exit;; *) echo "Please answer yes or no.";; esac
done
fi
/usr/bin/apt-get "$#"
You can call this script apt-get which you place in /path/to/wrapper/bin and you update PATH in your profile to read PATH=/path/to/wrapper/bin:$PATH

Bash check if nvm installed

How can I detirmine if nvm (Node Version Manager) is installed in bash?
I already have it installed in my system but I haven't been able to make any bash script that can detect it. I am making a script that should be used by others which depends on nvm, so I want to output if it's not installed and exit if it isn't..
This doesn't work: https://stackoverflow.com/a/26759734/846348 it says that nvm isn't installed but the bash script can use nvm..
Would be nice if it supported Mac Terminal, Mac iTerm and Windows with Linux shell at least.
one can check with command -v nvm:
$ command -v nvm >/dev/null 2>&1 || { echo >&2 "nvm is required, but it's not installed. Aborting."; exit 1; }
The nvm install script checks if nvm is installed using roughly the following logic:
if [ -d "${HOME}/.nvm/.git" ]; then echo "nvm installed"; else echo "nvm not installed"; fi
This just checks if the directory ~/.nvm/.git exists.
To exit with failure if the directory ~/.nvm/.git does not exist, you could use:
if [ ! -d "${HOME}/.nvm/.git" ]; then exit; fi
Check if nvm installed using a Makefile
NVM_EXISTS := $(shell if [ -d "${HOME}/.nvm/.git" ]; then echo "nvm installed"; fi)
.PHONY: check
check:
ifndef NVM_EXISTS
$(error Please install nvm: https://github.com/nvm-sh/nvm)
endif
Note on nvm install.sh
The actual install script uses the following functions to determine the nvm installation directory (rather than assuming ${HOME}/.nvm). But if you are using the default location ${HOME}/.nvm, you can skip these checks.
nvm_default_install_dir() {
[ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm"
}
nvm_install_dir() {
if [ -n "$NVM_DIR" ]; then
printf %s "${NVM_DIR}"
else
nvm_default_install_dir
fi
}

Running programs only if they are installed, and ignoring them otherwise

When writing shell scripts, is the an idiom or swift way to run a program only if it is installed, and if it is not, just let it be (or handle the error in some other way apart from installing it)?
More specifically, I have a lot of servers which I access over ssh, and whenever I get a new server, I simply copy all my rc-files to it. The .zshrc starts tmux unless it is already running. Some of the servers (not all) do not have tmux installed. I do not want to install it because of disk space limitations, I do not want to have different rc-files for different servers, and I do not want my rc-files to be interrupted when executing them.
I have seen solutions involving apt-cache policy <package-name>, so I guess I could use that and pipe it to something like grep -e 'Installed: (none)', but that would assume that the server is running Debian or Ubuntu, which I can not do, and it would only work for packages that were installed with apt, not things I have installed in other ways.
command -v <command> is the common (and POSIX) way to check if a command could be executed (is executable and on the $PATH).
E.g:
command -v tmux >/dev/null &&
tmux a -t name
(>/dev/null since, if the command exists, its path will be printed to STDOUT.)
It could be nice to put it in a reusable function:
maybe() {
! command -v "${1}" >/dev/null ||
"$#"
}
Then one could use:
maybe tmux a -t name
And if tmux is available then tmux a -t name will be run, otherwise it’ll be silently ignored.
Or, if you want some feedback when a command is not available:
maybe() {
if command -v "${1}" >/dev/null
then
"$#"
else
printf 'Command "%s" not available, skipping\n' "${1}" >&2
fi
}
This might help-
1) Assuming tmux is available in PATH (as it must be executable)
isAvailable=$(type -P tmux)
if [[ -x $isAvailable ]]; then
...
2) Verify file is present on a specific path (Copying all rc-files)
export FILEPATH="..."
if[[ -f $FILEPATH ]]; then

gcc and w32api not found

can anybody help me why these packages showed unavailable even they are installed?
I am trying to install NS2.3.5 on windows 10 64bit using cygwin.
as known, the install script of ns will check for required package in cygwin which are installed:
packages_base="gcc gcc-g++ gawk tar gzip make patch perl w32api"
packages_xorg="xorg-server xinit libX11-devel libXmu-devel"
you may notice that I modified the script to check for gcc instead of gcc4 and gcc-g++ instead of gcc4-g++, since the gcc4 is obsolete.
I also run the command gcc -dumpversion and I got the version 4.9.3
the basic command to check the package is:
cygcheck -c gcc
and the expected output is:
Package version Status
gcc-g++ 4.9.3-1 OK
however, the script that checks the packages failed to find gcc and w32api even they are installed. all other packages including gcc-g++ were checked successfully and get the exact version.
Okay,
Tried at my end ( as I already have a cygwin package installed ).
To me, this is more of an issue of "cygcheck" utility than anything else.
At my end too "cygcheck" failed to report details in proper for "gcc", with command "cygcheck -c gcc | grep gcc"
I am suggesting a trick here to over come this, but its just a trick.
In script "install" from "ns-2.35", find a function "test_packages" and change it something like below
test_packages() {
for i in $#; do
echo -n "Checking for ${i}... ";
cygcheck -c ${i} | grep ${i} >/dev/null 2>&1;^M
if [ "$?" -eq "0" ]; then
echo "ok";
else^M
cygcheck -l | grep ${i} >/dev/null 2>&1;
if [ "$?" -eq "0" ]; then
echo "ok";
else^M
echo "NO!";^M
echo "";
echo "Package ${i} is not present on your system.";
echo "";
echo "Please install it using Cygwin's setup.exe";
echo "before trying to install the ns-2 distribution.";
fi;
test_proceed;
fi;
done;
}
Basically rechecking again with "cygcheck -l" after first fail.
This passed the test but, I did not go further with installation.
Also there is a link which explains the installation os ns-2.35 on windows
which could be useful too.
http://www.slideshare.net/TBear76/ns235-installation-3395974
Please try out the 'Nov 2014 update', ns-allinone-2.35_gcc482.tar.gz
https://drive.google.com/file/d/0B7S255p3kFXNSGJCZ2YzUGJDVk0/view?usp=sharing
Can use all gcc/g++ versions 4.4.x .. 5.2.0
ns2

check if gpg-agent is installed from bash

I'm looking for the best way to check if gpg-agent is installed in a a machine.
I need to check from a shell script.
Thank you.
You may have to modify the path. Tested with RHEL 6 and 7.
if test -x /usr/bin/gpg-agent; then echo installed; else echo not installed; fi
Or:
if [ -x /usr/bin/gpg-agent ]; then echo insatlled; else echo not installed; fi
Further Reading: help test.

Resources