Conditional wrap command with execline - s6

How can I write this with execline ?
#!/bin/bash
if [ "${SOME_VAR}" = "some_value" ]
then
wrapper="cmd_wraps"
else
wrapper=""
fi
${wrapper} real_cmd
I did not succeed using ifthenelse.
Edit:
I found this, but is there better ?
#!/bin/execlineb
importas -D "" some_var SOME_VAR
backtick WRAPPER {
ifelse { s6-test "${some_var}" = "some_value" }
{ echo "cmd_wraps" }
echo "exec"
}
importas wrapper WRAPPER
${wrapper} my_cmd

Related

Bash shell if operator

I write a function like this to .commands file, and I imported .commands in .zshrc
function testA() {
echo "start"
if [ "$1" == "a" ]; then
echo "ok"
fi
echo "end"
}
And when I run testA a in terminal
start
testA:2: = not found
What is the problem here?
Chapter 12 – "Conditional Expressions" of the zsh documentation states:
A conditional expression is used with the [[ compound command to test attributes of files and to compare strings.
This means, changing your conditional expression to use [[ ... ]] instead of [ ... ] should make it work:
function testA() {
echo "start"
if [[ "$1" == "a" ]]; then
echo "ok"
fi
echo "end"
}
You can simplify the problem to simply type a [a == a] or echo ==. This will also produce this error. The reason is that a = has specific meaning to zsh, unless it is followed by a white space.
You have three possible workarounds:
Either quote that parameter, i.e.
[ $1 "==" a ]
or use a single equal sign, i.e.
[ $1 = a ]
or use [[, which introduces a slightly different parsing context:
[[ $1 == a ]]
$ cat fun.sh
function test() {
echo "start"
if [ "a" == "a" ]; then
echo "ok"
fi
echo "end"
}
Source script file
$ source fun.sh
Output:
$ test
start
ok
end

ZSH Custom function like repeat

I have a command which I want to repeat when a certain error occurs. To make this generic I would like to come up with a function that can take any other function to basically wrap that behaviour, very similar to repeat in ZSH.
So what I would like to have is something like this:
repeatWhenError { someFunction() }
This would repeat the function within the braces until it succeeds successfully. Is there an easy way to implement this in ZSH?
From my dotfiles:
retry () {
retry-limited 0 "$#"
}
retry-limited () {
retry-limited-eval "$1" "$(gquote "${#:2}")"
}
retry-limited-eval () {
local retry_sleep="${retry_sleep:-0.1}"
local limit=0
local ecode=0
until {
test "$1" -gt 0 && test $limit -ge "$1"
} || {
eval "${#:2}" && ecode=0
}
do
ecode="$?"
ecerr Tried eval "${#:2}" "..."
sleep $retry_sleep
limit=$((limit+1))
done
return "$ecode"
}
gquote () {
\noglob : Use this to control quoting centrally.
print -r -- "${(q+#)#}"
}
ecerr () {
print -r -- "$#" >&2
}
Usage:
retry someFunction

How to print return code conditionally in oh my zsh theme?

I'm trying to conditionally print return code in custom oh-my-zsh theme but somehow I'm failling. I'm stuck with:
function get_rc {
rc=$(echo -e "${return_code}" | tr -d '[:space:]' )
[[ "${rc}" == "130" ]] && echo "${rc}" && return
echo "'${rc}'"
}
Where it's printing the quouted version all the time.
What's wrong with my approach?

zsh errors in dotfiles

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

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"

Resources