file_name=index.html
pattern=TODO
for file_path in $(find -type f -name $file_name); do
dir=$(basename $(dirname $file_path))
word=${dir^}
sed -i -e "s/$pattern/$word/g" $file_path
done
Tag: shell
Get java version string via shell commands
Determine the pure java version string from any Unix/Linux shell (including Cygwin):
java -version 2>&1 | head -n 1 | cut -d'"' -f2
This requires only the very commonly available and lightweight “head” and “cut” commands.
I originally found the one-liner on stackoverflow. Thanks to the friendly folks who shared it.
To get only the major version part (e.g. 8 for Java 1.8.x, 11 for 11.x), use this:
java -version 2>&1 \
| head -1 \
| cut -d'"' -f2 \
| sed 's/^1\.//' \
| cut -d'.' -f1
Note: The sed step is required for versions up to Java 8 that start with the “1.” prefix.
Example: Ensure Java 11 or higher:
#!/bin/bash
version=$(java -version 2>&1 \
| head -1 \
| cut -d'"' -f2 \
| sed 's/^1\.//' \
| cut -d'.' -f1
)
if [ $version -lt "11" ]; then
echo "Java 11 or higher is required."
exit 1
fi
Use standard bash for ssh login on shared hosts
Please note: The following instructions are for ssh logins on a remote host. The approach is not suitable for executing remote commands via ssh.
Problem
When you work on a remote Linux or Unix server (via ssh) you sometimes cannot control your login shell and/or its default config file. For example, you might be sharing the same user account on the server with other people or the use of the chsh tool might be locked down.
Maybe the default shell is something like ksh, or if bash is used maybe the .bashrc sets vi key bindings. This can be annoying if you are used to standard Linux bash with its default Emacs style bindings.
Suggested solution
In these cases you can do the following, assuming bash is installed and in the path on the host:
1) Create /usr/local/bin/sbash.sh (on Windows, use Cygwin).:
#! /bin/bash
set -x
if [ "$#" -gt 0 ]; then
user_at_host="$1"
shift
ssh_options="$@"
else
set +x
echo Usage: $(basename "$0") user@host [ssh-options]
exit 1
fi
ssh -t $ssh_options \
$user_at_host \
"bash --rcfile .bashrc.for-remote-user-${USER}"
Make the file executable using something like chmod ugo+x /usr/local/bin/sbash.sh. You can then use it for remote logins like the ssh command, for example:
oliver@basement:~$ sbash.sh user@host
The $USER variable in the sbash.sh script will be substituted by the local shell with your local user name, which in this example is “oliver”.
2) On the host create ~/.bashrc.for_remote_user_USERNAME where USERNAME is the user name from the ssh client as mentioned above:
user@host$ vim $HOME/.bashrc.for-remote-user-oliver
Make sure this file name matches the –rcfile option in your ssh command.
You can then edit and use this file like a normal .bashrc file, i.e. for setting your favorite environment variables, bash options, aliases, etc.
