keeping track of a moving shell script keeping track of a moving shell script unix unix

keeping track of a moving shell script


For clarity, I would probably declare named variables for your common values instead of constantly reusing the ${0} array. It's also good practice to quote variables and strings.

The only major issue I saw, was running ./script.sh would make $0 equal just the filename, so I add "./" to the beginning in that case.

#!/bin/bash -uME="${0}"if [[ ! "$ME" =~ /^\// ]]; then        ME="./$ME"fiPARENT="${ME%/*}"FILENAME="${ME##*/}"FOLDER="SomeNewFolder"NEW="$PARENT/$FOLDER"if [[ ! -d "$NEW" ]] && [[ "${PARENT%/*}" != "$FOLDER" ]]; then        mkdir "$NEW"        mv "$ME" "$NEW"fiecho "$PARENT"echo "$NEW"


Well, you could do something like this to get an absolute path:

PARENTPATH=$( cd "$( dirname "$0" )" && pwd )NEWPATH=${PARENTPATH}/SomeNewFolder


me="$0"newdir=SomeNewFolderif [[ $me =~ ^/ ]] ; then    full_path="$me"else    full_path="$PWD/$me"fifull_path="${full_path//\/\.\///}" # prettifypath_to_me="${full_path%/*}"parent_dir="${path_to_me##*/}"if [ ! "$parent_dir" = "$newdir" ] ; then    mkdir -p "$path_to_me/$newdir"    mv -f "$full_path" "$path_to_me/$newdir/"fi

Basically similar to what lunixbochs was doing, but with a few minor alterations

  1. lower case variable names so as not to be confused with environment variables
  2. crudely estimates absolute path
  3. -f and -p becuase interactivity is never cool, and why not