Archive

Posts Tagged ‘shell’

Replace TODO in index files with Capitalized folder name

September 2, 2017 Leave a comment
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

Advertisements
Categories: bash, coding, html Tags: , ,

Get java version string via shell commands

July 27, 2014 Leave a comment

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.

The above gets the version from the java command on the PATH.
For other java binaries on your system, use the full path, like:

/full/path/to/some/java -version 2>&1 | head -n 1 | cut -d'"' -f2

To get only the major version part (e.g. 7 for Java 1.7.x), use this:

java -version 2>&1 | head -n 1 | cut -d'"' -f2 | cut -d'.' -f2 

Example: Ensure Java 7 or higher:

#!/bin/bash

version=$(java -version 2>&1 | head -n 1 | cut -d'"' -f2 | cut -d'.' -f2)
if [ $version -lt "7" ]; then
  echo "Java 1.7 or higher is required."
  exit 1
fi
Categories: bash, coding, cygwin, debian, java, linux, mac os Tags: , ,

Use standard bash for ssh login on shared hosts

January 9, 2014 Leave a comment

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.

Categories: bash, cygwin, debian Tags: ,