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.
Related
After installation on Ubuntu, mvn -version prints:
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE
I detected that the problem is in
if [ ! -x "$JAVACMD" ] ; then
echo "The JAVA_HOME environment variable is not defined correctly" >&2
echo "This environment variable is needed to run this program" >&2
echo "NB: JAVA_HOME should point to a JDK not a JRE" >&2
exit 1
fi
When I remove quotes from "$JAVACMD" it works perfectly
I saw that other scripts use commands with in quotes and I doubt that Maven released this script with errors. So, what seems to be the problem? Why my script won't work in original version? Type of script is #!/bin/sh but I tested this condition separately in new file with bash script. The result is the same. When I ask with quotes is command executable, result is false. When I ask without quotes is true
Edit:
I put directly that case in new bash file. The result is interesting
#!/bin/bash
JAVACMD1="$JAVA_HOME/bin/java"
JAVACMD2="/usr/lib/jvm/java-8-openjdk-amd64/bin/java"
echo $JAVACMD1
echo $JAVACMD2
if [ ! -x "$JAVACMD1" ]
then
echo "NOT"
else
echo "YES"
fi
if [ ! -x $JAVACMD1 ]
then
echo "NOT"
else
echo "YES"
fi
if [ ! -x "$JAVACMD2" ]
then
echo "NOT"
else
echo "YES"
fi
Result:
/usr/lib/jvm/java-8-openjdk-amd64/bin/java
/usr/lib/jvm/java-8-openjdk-amd64/bin/java
NOT
YES
YES
The problem was in env variable JAVA_HOME. It started with one blank, but that I didn't see in echo command (I suppose it trims the string). In command prompt I also used echo, but when I listed all env variables I saw it
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
}
I'm just learning to write bash scripts.
I'm writing a script to setup a new server.
How should I go about testing the script.
i.e.
I use apt install for certain packages like apache, php etc. and then a couple of lines down there is an error.
I then need to fix the error and run it again but it will run all the install commands again.
The system will probably say the package is installed already, but what if there are commands which append strings to files.
If these are run again it will append the same string to the file a second time.
What is the best approach to write bash-scripts like this?
Can you do test runs which rollback everything after an error or end of the script?
Or even better to have the script continue from the line where the error occured the next time it is run?
I'm doing this on an Ubuntu 18.04 server.
it's a matter of how clear you want it to be to read it, but
[ -f .step01-done ] || your install command && touch .step01-done
[ -f .step02-done ] || your other install command && touch .step02-done
maybe a little easier to read:
if ! [ -f .step01-done ]; then
if your install command ; then
touch .step01-done
fi
fi
if ! [ -f .step02-done ]; then
if your other install command ; then
touch .step02-done
fi
fi
...or something in between.
Now, I would suggest creating a directory somewhere and maybe logging output from the commands to some file there (maybe tee it) but definitely putting all these files you are creating with touch there. That way if you start it from another directory by accident, it won't matter. You just need to make sure that apt-get or whatever you use actual returns false if it fails. It should.
You could even make a function that does it in a nice way...
#!/bin/bash
function do_cmd() {
if [ -f "$1.done" ]; then
echo "$2: skipping already completed step"
return 0
fi
echo -n "$2: "
$3 1> "$1.out" 2> "$1.err"
if $?; then
echo "ok"
touch "$1.done"
return 0
else
echo "failed"
echo -e "see \"$1.out\" and/or \"$1.err\" for details."
return 1
# could "exit 1" instead
fi
}
[ -d /root/mysetup ] || mkdir /root/mysetup
if ! [ -d /root/mysetup ]; then
echo "failed to find or create /root/mysetup directory
exit 1
fi
cd /root/mysetup
# ---------------- your steps go here -------------------
do_cmd prog1 "installing prog1" "apt-get install prog1" || exit 1
do_cmd prog2 "installing prog2" "apt-get install prog2" || exit 1
do_cmd startfoo "starting foo service" "service foo start" || exit 1
echo "all setup functions finished."
You would use:
do_cmd identifier "description" "command or function"
description
identifier: unique identifier used when files are generated:
identifier.out: standard output from command
identifier.err: standard error from command
identifier.done: created when command is successful
description: this is actually printed to the terminal when the step is being executed.
command or function: this is the actual command to run
not sure why stackoverflow forced me to format that last bit as code but w/e
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
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