.husky/pre-commit: 2: .: Can't open .husky/_/husky.sh - husky

In a project, we use Husky but when I try to add a commit with git add . and git commit -m "test", I receive below error
.husky/pre-commit: 2: .: Can't open .husky/_/husky.sh
How can i fix this problem ?
My operating system is Windows but I use Ubuntu on this project
I have a pre-commit file and it is as below
Also the codes for husky.sh is below
if [ -z "$husky_skip_init" ]; then
debug () {
if [ "$HUSKY_DEBUG" = "1" ]; then
echo "husky (debug) - $1"
fi
}
readonly hook_name="$(basename "$0")"
debug "starting $hook_name..."
if [ "$HUSKY" = "0" ]; then
debug "HUSKY env variable is set to 0, skipping hook"
exit 0
fi
if [ -f ~/.huskyrc ]; then
debug "sourcing ~/.huskyrc"
. ~/.huskyrc
fi
export readonly husky_skip_init=1
sh -e "$0" "$#"
exitCode="$?"
if [ $exitCode != 0 ]; then
echo "husky - $hook_name hook exited with code $exitCode (error)"
fi
exit $exitCode
fi

for this problem, I have come up with a solution as below. In my IDE, line endings use CRLF by default. So I changed it to LF from the bottom right corner of the IDE and it worked

Related

Create menu from all users with /home/ directory in bash

I am trying to write a bash script to remove cookies and cache from installed browsers on shared Ubuntu machines. The problem I am facing is in creating a menu where you can select either ALL users or individual users.
I am trying to create a main menu that calls either of the 2 functions (a work in progress) to perform the tasks (I have commented out the commands to run for the meantime).
#!/bin/bash
# Remove Browser cache from Ubuntu 16.04 or Ubuntu 18.04
# Check running as root/sudo
if [ "$EUID" -ne 0 ] ;then
echo -e "Please run with;\nsudo $0"
exit
fi
# Enable extended globbing for the +(...) pattern
shopt -s extglob
## Check Ubuntu version
VERSION=$(lsb_release -d | awk -F":" '/Description/ {print $2}')
if [[ "$VERSION" = *"Ubuntu 18.04"* ]]; then
HOME_DIR="/home/ANT.DOMAIN.COM"
else
[[ "$VERSION" = *"Ubuntu 16.04"* ]]
HOME_DIR="/home/local/ANT"
fi
# Set Colours
RED='\033[1;31m'
YELLOW='\033[1;33m'
GREEN='\033[1;32m'
NC='\033[0m' # No Color
## Clear Browser Cache for ALL Users
clear_cache_all () {
mapfile -t PROFILES < <(find "$HOME_DIR" -mindepth 1 -maxdepth 1 -type d)
for PRO in "${PROFILES[#]}"
do
# Check FireFox installed
dpkg -s firefox &> /dev/null
if [ $? -eq 0 ]; then
#rm -rf "$PRO"/.mozilla/firefox/*.default/*.sqlite "$PRO"/.mozilla/firefox/*default/sessionstore.js
#rm -rf "$PRO"/.cache/mozilla/firefox/*.default/*
echo -e "FireFox Cookies & Cache Cleared for user ${GREEN}$USERNAME${NC}"
else
echo -e "${YELLOW}FireFox Not Installed...moving on${NC}"
fi
# Check Chromium installed
dpkg -s chromium-browser &> /dev/null
if [ $? -eq 0 ]; then
#rm -rf "$PRO"/.config/chromium/Default/
#rm -rf "$PRO"/.cache/chromium
echo -e "Chromium Cookies & Cache Cleared for user ${GREEN}$USERNAME${NC}"
else
echo -e "${YELLOW}Chromium Not Installed...moving on${NC}"
fi
# Check Chrome installed
dpkg -s google-chrome-stable &> /dev/null
if [ $? -eq 0 ]; then
#rm -rf "$PRO"/.config/google-chrome/Default/
#rm -rf "$PRO"/.cache/google-chrome
echo -e "Google Chrome Cookies & Cache Cleared for user ${GREEN}$USERNAME${NC}"
else
echo -e "${YELLOW}Google Chrome Not Installed...moving on${NC}"
fi
done
}
## Clear Cache for Individual Users
clear_cache_user () {
echo "stuff!"
}
# main menu function
main_menu () {
clear
if [ -d "$HOME_DIR" ]
then
mapfile -t USERS < <(find "$HOME_DIR" -mindepth 1 -maxdepth 1 -type d)
# Get basename for users
USERNAME="${USERS[#]##*/}"
string="#(${USERNAME[0]}"
for((i=1;i<${#USERNAME[#]};i++))
do
string+="|${USERNAME[$i]}"
done
string+=")"
select NAME in "Clear ALL" "${USERNAME[#]}" "Quit"
do
case $NAME in
"Clear ALL")
# Call clear_cache_all Function
clear_cache_all
exit
;;
$string)
# Call clear_cache_user Function
clear_cache_user
;;
"Quit")
exit
;;
*)
echo "Invalid option, please try again";;
esac
done
else
echo -e "${RED}Error: Cannot find home directories...exiting${NC}"
fi
}
### SCRIPT COMMANDS ###
main_menu
Ok, so I can think of two options for your problem. I'll try to follow the names of your variables.
As I can see in your code, you have already put in the variable "string" all the usernames, so my first idea is to use a read and a simple if:
read -P "Insert ALL for all users, the Username for a single user, or Quit to exit: " NAME
if [ $NAME = "ALL" ]
then
clear_cache_all
exit
elif [ $NAME = "Quit" ]
then
echo "Bye!"
exit
else
for i in "${string[#]}"
do
if [ "$i" == "$NAME" ] ; then
clear_cache_user($NAME) #Guessing you'll pass the username as a variable to the function
exit
fi
done
echo "Invalid option, please try again"
fi
The other option is to use the case statement, as you were using. The problem is that case doesn't work easy with arrays, so while it's "case / in", it doesn't mean it's checking if the variable is an element of the array. In case you are forced to use case (or are in love with it), check this two links for some solutions: this one and this one.
Hope this helps! Good luck!

Editing file with vim without typing path (similar to autojump)

A few months back, I installed a utility on my mac so that instead of typing something like this:
vim /type/path/to/the/file
I could just type:
v file
9 times out of 10 it would guess the right file based on the past history, similar to the way autojump works. And instead of typing in vim I can just type the letter v.
I can't remember how I set this up though. It still works on my mac but I don't see anything in my .bash_profile that shows how I did that.
I'm trying to get this to work on my linux box.
This can be found here
https://github.com/rupa/v/blob/master/v
it should work in Linux too. It is a bash script that uses the viminfo
history file to fill in partial strings.
It can be installed on macOS with brew install v
Ah! I found the command with which. Here is the magical script. I can't determine where I got it.
#!/usr/bin/env bash
[ "$vim" ] || vim=vim
[ $viminfo ] || viminfo=~/.viminfo
usage="$(basename $0) [-a] [-l] [-[0-9]] [--debug] [--help] [regexes]"
[ $1 ] || list=1
fnd=()
for x; do case $x in
-a) deleted=1;;
-l) list=1;;
-[1-9]) edit=${x:1}; shift;;
--help) echo $usage; exit;;
--debug) vim=echo;;
--) shift; fnd+=("$#"); break;;
*) fnd+=("$x");;
esac; shift; done
set -- "${fnd[#]}"
[ -f "$1" ] && {
$vim "$1"
exit
}
while IFS=" " read line; do
[ "${line:0:1}" = ">" ] || continue
fl=${line:2}
[ -f "${fl/\~/$HOME/}" -o "$deleted" ] || continue
match=1
for x; do
[[ "$fl" =~ $x ]] || match=
done
[ "$match" ] || continue
i=$((i+1))
files[$i]="$fl"
done < "$viminfo"
if [ "$edit" ]; then
resp=${files[$edit]}
elif [ "$i" = 1 -o "$list" = "" ]; then
resp=${files[1]}
elif [ "$i" ]; then
while [ $i -gt 0 ]; do
echo -e "$i\t${files[$i]}"
i=$((i-1))
done
read -p '> ' CHOICE
resp=${files[$CHOICE]}
fi
[ "$resp" ] || exit
$vim "${resp/\~/$HOME}"

Shell Mount and check directionairy existence

Just looking for some help with my mounting shell script, wondering if anyone could advice me on how to make it check for the directory at the mount point exists and is empty, or is created by the script if it does not exist
#!/bin/bash
MOUNTPOINT="/myfilesystem"
if grep -qs "$MOUNTPOINT" /proc/mounts; then
echo "It's mounted."
else
echo "It's not mounted."
mount "$MOUNTPOINT"
if [ $? -eq 0 ]; then
echo "Mount success!"
else
echo "Something went wrong with the mount..."
fi
fi
Your use of grep will return any mountpoint that contains the string /myfilesystem in... e.g: both of these:
/myfilesystem
/home/james/myfilesystem
Prefer to use something more prescriptive like the following:
mountpoint -q "${MOUNTPOINT}"
You can use [ to test if a path is a directory:
if [ ! -d "${MOUNTPOINT}" ]; then
if [ -e "${MOUNTPOINT}" ]; then
echo "Mountpoint exists, but isn't a directory..."
else
echo "Mountpoint doesn't exist..."
fi
fi
mkdir -p will create all parent directories, as necessary:
mkdir -p "${MOUNTPOINT}"
Finally, test if a directory is empty by exploiting bash's variable expansion:
[ "$(echo ${MOUNTPOINT}/*)" != "${MOUNTPOINT}/*" ]
It's also a good idea to run scripts with some level of 'safety'. See the set built-in command: https://linux.die.net/man/1/bash
-e Exit immediately if a pipeline (which may consist of a single simple command), a
list, or a compound command (see SHELL GRAMMAR above), exits with a non-zero
status.
-u Treat unset variables and parameters other than the special parameters "#" and "*"
as an error when performing parameter expansion.
In full: (note bash -eu)
#!/bin/bash -eu
MOUNTPOINT="/myfilesystem"
if [ ! -d "${MOUNTPOINT}" ]; then
if [ -e "${MOUNTPOINT}" ]; then
echo "Mountpoint exists, but isn't a directory..."
exit 1
fi
mkdir -p "${MOUNTPOINT}"
fi
if [ "$(echo ${MOUNTPOINT}/*)" != "${MOUNTPOINT}/*" ]; then
echo "Mountpoint is not empty!"
exit 1
fi
if mountpoint -q "${MOUNTPOINT}"; then
echo "Already mounted..."
exit 0
fi
mount "${MOUNTPOINT}"
RET=$?
if [ ${RET} -ne 0 ]; then
echo "Mount failed... ${RET}"
exit 1
fi
echo "Mounted successfully!"
exit 0
Here is how can you check directory exist and it is empty:
if [ -d /myfilesystem ] && [ ! "$(ls -A /myfilesystem/)" ]; then
echo "Directory exist and it is empty"
else
echo "Directory doesnt exist or not empty"
fi

Linux - Put a custom script in custom load state in SDDM

appreciate if you can help. I'm trying to move my Google Chrome cache to RAM-disk, and I doing it following the instruction. It says, I need to put script in my display manager config this way:
GDM (Gnome Display Manager) or MDM - /etc/mdm/PostSession/Default
add "/etc/init.d/ramdisksaverestore stop" before line "exit 0"
LightDM - /etc/lightdm/lightdm.conf
add "session-cleanup-script = /etc/init.d/ramdisksaverestore stop" in the end of "[SeatDefaults]" section.
But I use Linux Mint 18 KDE (Ubuntu-based) with SDDM, and have no idea where should I put my script (I mean both file and line of it). Any help?
Maybe I should put that in the /usr/share/sddm/scripts/Xsession but where and how?
#! /bin/sh
# Xsession - run as user
# This file is extracted from kde-workspace (kdm/kfrontend/genkdmconf.c)
# Copyright (C) 2001-2005 Oswald Buddenhagen <ossi#kde.org>
session=$1
# Note that the respective logout scripts are not sourced.
case $SHELL in
*/bash)
[ -z "$BASH" ] && exec $SHELL $0 "$#"
set +o posix
[ -f /etc/profile ] && . /etc/profile
if [ -f $HOME/.bash_profile ]; then
. $HOME/.bash_profile
elif [ -f $HOME/.bash_login ]; then
. $HOME/.bash_login
elif [ -f $HOME/.profile ]; then
. $HOME/.profile
fi
;;
*/zsh)
[ -z "$ZSH_NAME" ] && exec $SHELL $0 "$#"
[ -d /etc/zsh ] && zdir=/etc/zsh || zdir=/etc
zhome=${ZDOTDIR:-$HOME}
# zshenv is always sourced automatically.
[ -f $zdir/zprofile ] && . $zdir/zprofile
[ -f $zhome/.zprofile ] && . $zhome/.zprofile
[ -f $zdir/zlogin ] && . $zdir/zlogin
[ -f $zhome/.zlogin ] && . $zhome/.zlogin
emulate -R sh
;;
*/csh|*/tcsh)
# [t]cshrc is always sourced automatically.
# Note that sourcing csh.login after .cshrc is non-standard.
xsess_tmp=`mktemp /tmp/xsess-env-XXXXXX`
$SHELL -c "if (-f /etc/csh.login) source /etc/csh.login; if (-f ~/.login) source ~/.login; /bin/sh -c 'export -p' >! $xsess_tmp"
. $xsess_tmp
rm -f $xsess_tmp
;;
*) # Plain sh, ksh, and anything we do not know.
[ -f /etc/profile ] && . /etc/profile
[ -f $HOME/.profile ] && . $HOME/.profile
;;
esac
[ -f /etc/xprofile ] && . /etc/xprofile
[ -f $HOME/.xprofile ] && . $HOME/.xprofile
# run all system xinitrc shell scripts.
if [ -d /etc/X11/xinit/xinitrc.d ]; then
for i in /etc/X11/xinit/xinitrc.d/* ; do
if [ -x "$i" ]; then
. "$i"
fi
done
fi
# Load Xsession scripts
# OPTIONFILE, USERXSESSION, USERXSESSIONRC and ALTUSERXSESSION are required
# by the scripts to work
xsessionddir="/etc/X11/Xsession.d"
OPTIONFILE=/etc/X11/Xsession.options
USERXSESSION=$HOME/.xsession
USERXSESSIONRC=$HOME/.xsessionrc
ALTUSERXSESSION=$HOME/.Xsession
if [ -d "$xsessionddir" ]; then
for i in `ls $xsessionddir`; do
script="$xsessionddir/$i"
echo "Loading X session script $script"
if [ -r "$script" -a -f "$script" ] && expr "$i" : '^[[:alnum:]_-]\+$' > /dev/null; then
. "$script"
fi
done
fi
if [ -d /etc/X11/Xresources ]; then
for i in /etc/X11/Xresources/*; do
[ -f $i ] && xrdb -merge $i
done
elif [ -f /etc/X11/Xresources ]; then
xrdb -merge /etc/X11/Xresources
fi
[ -f $HOME/.Xresources ] && xrdb -merge $HOME/.Xresources
case $session in
"")
exec xmessage -center -buttons OK:0 -default OK "Sorry, $DESKTOP_SESSION is no valid session."
;;
*)
eval exec "$session"
;;
esac
exec xmessage -center -buttons OK:0 -default OK "Sorry, cannot execute $session. Check $DESKTOP_SESSION.desktop."
well, it turns out the best decision is to use PSD ( https://wiki.archlinux.org/index.php/profile-sync-daemon) and do not worry about anything, it will solve all the problems by itself.

Why is the $ sign used in this bash script

I'm looking on Fedora sysV init script example and it goes like this:
#...some code
start() {
[ -x $exec ] || exit 5
[ -f $config ] || exit 6
echo -n $"Starting $prog: "
# if not running, start it up here, usually something like "daemon $exec"
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
#...some more code....
What is the reason for first dollar sign in this line, because it seems that script will work perfectly fine without it - will it not?
echo -n $"Starting $prog: "
$"..." is an extension that allows you to use gettext i18n in bash scripts.

Resources