zsh errors in dotfiles - zsh

I am trying to set up a new work environment, using zsh and an old set of dotfiles. I have this error with git information and I do not know how to solve it. Here is the error from iTerm2 as soon as I cd into my dotfiles repo:
~ ✔ cd dotfiles
_git_prompt_color:3: = not found
dotfiles ✔ _git_prompt_color:3: = not found
I am assuming the issue surrounds line 3 of the _git_prompt_color method?
Here is the file where I think the issue is:
_git_prompt_info() {
ref=$(git symbolic-ref HEAD 2> /dev/null)
if [ -n $ref ]; then
branch_name="${ref#refs/heads/}"
branch_name_max_length=$(($COLUMNS/5))
if [ ${#branch_name} -gt $branch_name_max_length ]; then
echo "$branch_name[0,$(($branch_name_max_length-3))]..."
else
echo $branch_name
fi
fi
}
_git_status() {
git_status=$(cat "/tmp/git-status-$$")
if [ -n "$(echo $git_status | grep "Changes not staged")" ]; then
echo "changed"
elif [ -n "$(echo $git_status | grep "Changes to be committed")" ]; then
echo "pending"
elif [ -n "$(echo $git_status | grep "Untracked files")" ]; then
echo "untracked"
else
echo "unchanged"
fi
}
_git_prompt_color() {
if [ -n "$1" ]; then
current_git_status=$(_git_status)
if [ "changed" == $current_git_status ]; then
echo "$(_red $1)"
elif [ "pending" == $current_git_status ]; then
echo "$(_yellow $1)"
elif [ "unchanged" == $current_git_status ]; then
echo "$(_green $1)"
elif [ "untracked" == $current_git_status ]; then
echo "$(_cyan $1)"
fi
else
echo "$1"
fi
}
_color() {
if [ -n "$1" ]; then
echo "%{$fg_bold[$2]%}$1%{$reset_color%}"
fi
}
_separate() { if [ -n "$1" ]; then echo " $1"; fi }
_grey() { echo "$(_color "$1" grey)" }
_yellow() { echo "$(_color "$1" yellow)" }
_green() { echo "$(_color "$1" green)" }
_red() { echo "$(_color "$1" red)" }
_cyan() { echo "$(_color "$1" cyan)" }
_blue() { echo "$(_color "$1" blue)" }
_full_path() { echo "$(_blue "%~")" }
_working_directory() { echo "$(_blue "%c")" }
_colored_git_branch() { echo "$(_git_prompt_color "$(_git_prompt_info)")" }
_display_current_vim_mode() {
if [[ $VIMODE == 'vicmd' ]]; then
echo "$(_red "✘")"
else
echo "$(_green "✔")"
fi
}
function zle-line-init zle-keymap-select {
VIMODE=$KEYMAP
zle reset-prompt
}
zle -N zle-line-init
zle -N zle-keymap-select
function precmd {
$(git status 2> /dev/null >! "/tmp/git-status-$$")
}
PROMPT='$(_working_directory)$(_separate $(_colored_git_branch)) $(_display_current_vim_mode) '
Any help on this issue would be much appreciated!

[ do not support the == operator. You've to either use = or perform the comparison using [[ operator. [ offers only limited functionality but it's POSIX compliant, While [[ isn't.
Here it would be
if [[ "changed" == $current_git_status ]]
or
if [ "changed" = $current_git_status ]
Difference between test, [ and [[
Tip :
Try using vcs_info to obtain basic version control information.
vcs_info documentation
vcs_info examples

Related

test ./script.sh doit test dont show true output

#!/usr/bin/env zsh
doit() {
if [[ "$1" = "start" ]]; then
for loc in $(cat all-doc); do
if ! screen -list | grep -q My-$loc; then
screen -dmS My-$loc /home/Server -f /home/$loc.cfg
fi
done
elif [[ "$1" = "stop" ]]; then
for loc in $(cat all-doc); do
if screen -list | grep -q My-$loc; then
pkill -f My-$loc;
fi
done
else
echo "Option: ERROR..."
fi
}
nothing() {
if [[ "$1" = "start" ]]; then
echo "Option: 1"
elif [[ "$1" = "stop" ]]; then
echo "Option: 2"
else
echo "Option: 3"
fi
}
case "$2" in
start)
"$1" "$2";
;;
stop)
"$1" "$2";
;;
restart)
restart;
;;
*)
echo "Usage: $0 {doit|nothing} {start|stop|restart}"
exit 1
;;
esac
exit 0
Output:
./script.sh:34> case test (start)
./script.sh:34> case test (stop)
./script.sh:34> case test (restart)
./script.sh:34> case test (*)
./script.sh:45> echo 'Usage: ./script.sh {start|stop|restart}'
sage: ./script.sh {start|stop|restart}
./script.sh:46> exit 1
this script for start and stop and restart my servers.
If $2 not match with "start" "stop" "restart" in both function must call else but not work.
Ok question is why ./script.sh doit test did not call
else
echo "Option: ERROR..."
whats the sulotion ? Is there better way to do somthing for ./script.sh $1 $2 i mean $1 get function and $2 get start|stop|restart ?

Unix run a script with -help option

I have the below script that is expected to work when the user invokes sh <scriptName> <propertyfile> It does work when I provide this at the dollar prompt. However, I am having two issues with the script.
If I provide just one argument, ie if I do - sh <scriptName>, I see the below error -
my-llt-utvsg$ sh temp.sh
Usage temp.sh
When I do -help, I see the below error -
my-llt-utvsg$ sh tmp.sh -help
-help does not exist
What am I doing wrong? Can someone please advise? I am a software developer that very rarely needs to do shell scripting, so please go easy on me ;)
#!/bin/bash
FILE="system.properties"
FILE=$1
if [ ! -f $FILE ];
then
echo "$FILE does not exist"
exit
fi
usage ()
{
echo "Usage $0 $FILE"
exit
}
if [ "$#" -ne 1 ]
then
usage
fi
if [ "$1" = "-help" ] ; then
echo ""
echo '############ HELP PROPERTIES ############ '
echo ""
echo 'Blah.'
exit
The reason your
if [ "$1" = "-help" ] ; then
check is not working is that it only checks $1 or the first argument.
Try instead:
for var in "$#"
do
if [ "$var" = "-help" ] ; then
echo ""
echo '############ HELP PROPERTIES ############ '
echo ""
echo 'Blah.'
fi
done
Which will loop over each argument and so will run if any of them are -help.
Try this as well:
#!/bin/bash
FILES=()
function show_help_info_and_exit {
echo ""
echo '############ HELP PROPERTIES ############ '
echo ""
echo 'Blah.'
exit
}
function show_usage_and_exit {
echo "Usage: $0 file"
exit
}
for __; do
if [[ $__ == -help ]]; then
show_help_info_and_exit
elif [[ -f $__ ]]; then
FILES+=("$__")
else
echo "Invalid argument or file does not exist: $__"
show_usage_and_exit
fi
done
if [[ ${#FILES[#]} -ne 1 ]]; then
echo "Invalid number of file arguments."
show_usage_and_exit
fi
echo "$FILES"

Unix elif syntax error

Getting error:
syntax error near unexpected token `elif'
`elif [ "$PROJECT_DIR" = "Automation" ] then'
I'm saving in Unix line format in Textpad++ so I'm not sure what the problem is...
#!/bin/bash
....
if [ "$PROJECT_DIR" = "Utility" ] then
echo "$PROJECT_DIR: Compiling..."
javac -sourcepath $SOURCE -classpath $CLASSPATH -d $OUTPUT $SOURCE/*.java -deprecation
echo "$PROJECT_DIR: Compiling..."
javac -sourcepath $SOURCE -classpath $CLASSPATH -d $OUTPUT $SOURCE/*.java -deprecation
elif [ "$PROJECT_DIR" = "Automation" ] then
echo "$PROJECT_NAME: Compiling..."
javac -sourcepath $SOURCE -classpath $CLASSPATH -d $OUTPUT $SOURCE/*.java
elif [ "$PROJECT_DIR" = "Sync" ] then
echo "$PROJECT_DIR: Compiling..."
javac -sourcepath $SOURCE -classpath $CLASSPATH -d $OUTPUT $SOURCE/*.java -deprecation
echo "$PROJECT_DIR: Compiling..."
javac -sourcepath $SOURCE -classpath $CLASSPATH -d $OUTPUT $SOURCE/*.java -deprecation
echo "$PROJECT_DIR: Compiling..."
javac -sourcepath $SOURCE -classpath $CLASSPATH -d $OUTPUT $SOURCE/*.java -deprecation
fi
The problem is that you need a semicolon (or a newline) between the ] and the then.
[ is not part of the shell's syntax. It's a built-in command, equivalent to the test command (except that [ requires a ] as its last argument and test doesn't).
(On the other hand, the keywords if, then, elif, and fi as well as the semicolon, are part of the shell's syntax.)
So if you write:
if [ "$PROJECT_DIR" = "Utility" ] then
the word then is simply another argument to the [ command.
Changing each occurrence of ] then to ] ; then should fix the problem.
Try like this instead (Removed most of the code lines to keep the answer short). no need of last elif condition elif [ "$PROJECT_DIR" = "Sync" ]. It should just be an else part as pointed below.
if [ "$PROJECT_DIR" = "Utility" ]
then
echo "$PROJECT_DIR: Compiling..."
elif [ "$PROJECT_DIR" = "Automation" ]
then
echo "$PROJECT_NAME: Compiling..."
else
echo "$PROJECT_DIR: Compiling..."
fi

zsh: parse error: condition expected: "$1"

I tried to write a script to compile java files with gentoos java-config but i ended up getting an error
zsh: parse error: condition expected: "$1" Can anyone tell me what this means and why it gets reported at line 16 in the function.
function jComp() {
local java_mods = ""
if (( $# == 0)); then
echo "using javac on .java in folder"
`javac *.java`
return 0
elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
echo "Usage: jComp [java modules] [file]"
echo
echo "Options:"
echo " -h, This help message."
echo "modules has to be in the (java-config -l) list"
echo
echo "Report bugs to <tellone.diloom#gmail.com>."
return 0
fi
if [[ "$(java-config -v)" == "" ]]; then
echo "This script depends on java-config"
return 1
elif [[ "$1" =="-d" ]] || [[ "$1" == "--default"]]; then
`javac -cp .:$(java-config -p junit-4) *.java`
if [[ $# == 2 ]]; then
`javac -c .:$(java-config -p junit-4) "$2"`
return 0
fi
fi
while (( $# > 1 )); do
if [[ ! -f "$1" ]]; then
java_mods="$java_mods $1"
shift
continue
fi
done
`javac -cp .:$(java-config $java_mods)`
return 0
}
Links and comment are welcome. Thanks in advance
It looks like your code is trying to compare a string stored in argument $1 to the string -d but the comparison is missing a space after the double equal sign:
elif [[ "$1" =="-d" ]] || [[ "$1" == "--default"]]; then
^
elif [[ "$1" == "-d" ]] || [[ "$1" == "--default"]]; then
I haven't tried the code but, do try and let me know if it solved it !
Btw, it also looks like the second comparison will also fail because of a lack of space before the double square closing brackets:
elif [[ "$1" == "-d" ]] || [[ "$1" == "--default"]]; then
^
elif [[ "$1" == "-d" ]] || [[ "$1" == "--default" ]]; then
All your backticked commands look wrong. You want to run the commands, not interpret their output as a command to run, right? If so, remove all the backticks from the javac invocations.
Then there is a missing space in [[ "$1" =="-d" ]] to make the == a separate token (and another as pointed out by leroyse).

Check that a variable is a number in UNIX shell [duplicate]

This question already has answers here:
How do I test if a variable is a number in Bash?
(40 answers)
Closed 1 year ago.
How do I check to see if a variable is a number, or contains a number, in UNIX shell?
if echo $var | egrep -q '^[0-9]+$'; then
# $var is a number
else
# $var is not a number
fi
Shell variables have no type, so the simplest way is to use the return type test command:
if [ $var -eq $var 2> /dev/null ]; then ...
(Or else parse it with a regexp)
No forks, no pipes. Pure POSIX shell:
case $var in
(*[!0-9]*|'') echo not a number;;
(*) echo a number;;
esac
(Assumes number := a string of digits). If you want to allow signed numbers with a single leading - or + as well, strip the optional sign like this:
case ${var#[-+]} in
(*[!0-9]*|'') echo not a number;;
(*) echo a number;;
esac
In either ksh93 or bash with the extglob option enabled:
if [[ $var == +([0-9]) ]]; then ...
Here's a version using only the features available in a bare-bones shell (ie it'd work in sh), and with one less process than using grep:
if expr "$var" : '[0-9][0-9]*$'>/dev/null; then
echo yes
else
echo no
fi
This checks that the $var represents only an integer; adjust the regexp to taste, and note that the expr regexp argument is implicitly anchored at the beginning.
This can be checked using regular expression.
###
echo $var|egrep '^[0-9]+$'
if [ $? -eq 0 ]; then
echo "$var is a number"
else
echo "$var is not a number"
fi
I'm kind of newbee on shell programming so I try to find out most easy and readable
It will just check the var is greater or same as 0
I think it's nice way to choose parameters... may be not what ever... :
if [ $var -ge 0 2>/dev/null ] ; then ...
INTEGER
if echo "$var" | egrep -q '^\-?[0-9]+$'; then
echo "$var is an integer"
else
echo "$var is not an integer"
fi
tests (with var=2 etc.):
2 is an integer
-2 is an integer
2.5 is not an integer
2b is not an integer
NUMBER
if echo "$var" | egrep -q '^\-?[0-9]*\.?[0-9]+$'; then
echo "$var is a number"
else
echo "$var is not a number"
fi
tests (with var=2 etc.):
2 is a number
-2 is a number
-2.6 is a number
-2.c6 is not a number
2. is not a number
2.0 is a number
if echo $var | egrep -q '^[0-9]+$'
Actually this does not work if var is multiline.
ie
var="123
qwer"
Especially if var comes from a file :
var=`cat var.txt`
This is the simplest :
if [ "$var" -eq "$var" ] 2> /dev/null
then echo yes
else echo no
fi
Here is the test without any regular expressions (tcsh code):
Create a file checknumber:
#! /usr/bin/env tcsh
if ( "$*" == "0" ) then
exit 0 # number
else
((echo "$*" | bc) > /tmp/tmp.txt) >& /dev/null
set tmp = `cat /tmp/tmp.txt`
rm -f /tmp/tmp/txt
if ( "$tmp" == "" || $tmp == 0 ) then
exit 1 # not a number
else
exit 0 # number
endif
endif
and run
chmod +x checknumber
Use
checknumber -3.45
and you'll got the result as errorlevel ($?).
You can optimise it easily.
( test ! -z "$num" && test "$num" -eq "$num" 2> /dev/null ) && {
# $num is a number
}
You can do that with simple test command.
$ test ab -eq 1 >/dev/null 2>&1
$ echo $?
2
$ test 21 -eq 1 >/dev/null 2>&1
$ echo $?
1
$ test 1 -eq 1 >/dev/null 2>&1
$ echo $?
0
So if the exit status is either 0 or 1 then it is a integer , but if the exis status is 2 then it is not a number.
a=123
if [ `echo $a | tr -d [:digit:] | wc -w` -eq 0 ]
then
echo numeric
else
echo ng
fi
numeric
a=12s3
if [ `echo $a | tr -d [:digit:] | wc -w` -eq 0 ]
then
echo numeric
else
echo ng
fi
ng
Taking the value from Command line and showing THE INPUT IS DECIMAL/NON-DECIMAL and NUMBER or not:
NUMBER=$1
IsDecimal=`echo "$NUMBER" | grep "\."`
if [ -n "$IsDecimal" ]
then
echo "$NUMBER is Decimal"
var1=`echo "$NUMBER" | cut -d"." -f1`
var2=`echo "$NUMBER" | cut -d"." -f2`
Digit1=`echo "$var1" | egrep '^-[0-9]+$'`
Digit2=`echo "$var1" | egrep '^[0-9]+$'`
Digit3=`echo "$var2" | egrep '^[0-9]+$'`
if [ -n "$Digit1" ] && [ -n "$Digit3" ]
then
echo "$NUMBER is a number"
elif [ -n "$Digit2" ] && [ -n "$Digit3" ]
then
echo "$NUMBER is a number"
else
echo "$NUMBER is not a number"
fi
else
echo "$NUMBER is not Decimal"
Digit1=`echo "$NUMBER" | egrep '^-[0-9]+$'`
Digit2=`echo "$NUMBER" | egrep '^[0-9]+$'`
if [ -n "$Digit1" ] || [ -n "$Digit2" ]; then
echo "$NUMBER is a number"
else
echo "$NUMBER is not a number"
fi
fi

Resources