Reputation: 87
I want to create shortcut for 'code file' (VS Code) if there's a second argument, or 'clear' if there's none in a single command line, but i don't know how the bash syntax works.
By looking at the mkcd (mkdir & cd) shortcut that i created:
function mkcd {
if [ ! -n "$1" ]; then
echo "Enter a directory name"
elif [ -d $1 ]; then
echo "\`$1' already exists"
else
mkdir $1 && cd $1
fi
}
I tried to do the same, but the error shows 'syntax error near unexpected token `else''
function c {
if $1 then
code $1
else
clear
fi
}
Upvotes: 0
Views: 561
Reputation: 203655
Your syntax error is that if $1 then
is missing a semi-colon (e.g. if $1; then
) but read on... There are no "shortcut"s in UNIX. There are scripts, functions, and aliases. It looks like you want to create a function named c
to call some command named code
when an argument is given, some other command named clear
otherwise. That'd be:
c() {
if (( $# > 0 )); then
code "$*"
else
clear
fi
}
Upvotes: 1