Bash

Bash Crib Sheet

Your browser can’t display this PDF. Download it instead.

Recent Git bash issue.

I recently had a issue with running bash scripts from Git-Bash. The python variant I was using being windows compiled required windows stlye paths not linux I needed to know which system the bash scripts were run from. (Linux,Windows Bash/ Windows Cywin) For this the uname function can be used from the Bash scripts.

# detect real bash

function LINUX?() {
    if [[ $(uname -s) == "Linux" ]]; then
	return 1
    else
	return 0
    fi
}


#detect git bash

function GITBASH?() {
    if [[ $(uname -s) == *"MINGW64"* ]]; then
	return 1
    else
	return 0
    fi
}

I also created sed based scripts to convert between both windows and linux paths, as the script used an environment varible to set a path reference.


# replacements

  rep_lin_path_start='\/c\/'
  rep_win_path_start='C\:\\'
  lin_dir='\/'
  win_dir='\\'
  wback='\.\.\\'
  lback='\.\.\/'
  empty=

function LIN2WINPATH () {
    local STR="$1"
    local var1=""
    var1=$(echo $STR | sed "s#${rep_lin_path_start}#${rep_win_path_start}#g" \
		     | sed "s#${lin_dir}#${win_dir}#g")
    echo $var1
}


function WIN2LINPATH () {
    local STR="$1"
    local var1=""
    var1=$(echo $STR | sed "s#${rep_win_path_start}#${rep_lin_path_start}#g" \
		     | sed "s#${win_dir}#${lin_dir}#g")
    echo $var1
}



# Will remove any ..\ references

function LIN2WINREALPATH () {
    local STR="$1"
    local var1=""
    var1=$(echo $STR | sed "s#${rep_lin_path_start}#${rep_win_path_start}#g" \
		     | sed "s#${lback}#/#g" \
		     | sed "s#${lin_dir}#${win_dir}#g") 
    echo $var1
}

# will remove any ../ references

function WIN2LINREALPATH () {
    local STR="$1"
    local var1=""
    var1=$(echo $STR | sed -e "s#${rep_win_path_start}#${rep_lin_path_start}#g" \
		     | sed -e "s#${wback}#/#g" \
		     | sed -e "s#${win_dir}#${lin_dir}#g")
    echo $var1
}


# Remote access using sudo and still get X windows to work

sudo xauth add $(xauth -f ~/.Xauthority list| tail -1)