Mac is getting slower. Because of pyenv or zsh, bash setting? - oh-my-zsh

Suddenly my terminal is getting slower seriously!
I don't know why but I guess the cause is that something wrong pyenv or zsh, bash setting.
Here is my pyenv log When I run ls in Terminal(zsh).
+_pyenv_virtualenv_hook:1> local 'ret=0'
+_pyenv_virtualenv_hook:2> [ -n '' ']'
+_pyenv_virtualenv_hook:5> pyenv sh-activate --quiet
+pyenv:1> local command
+pyenv:2> command=sh-activate
+pyenv:3> [ 2 -gt 0 ']'
+pyenv:4> shift
+pyenv:7> case sh-activate (activate | deactivate | rehash | shell)
+pyenv:7> case sh-activate (*)
+pyenv:11> pyenv sh-activate --quiet
+ [pyenv:23] enable -f /usr/local/Cellar/pyenv/20160202/libexec/../libexec/pyenv-realpath.dylib realpath
+ [pyenv:29] '[' -z '' ']'
++ [pyenv:31] type -p greadlink readlink
++ [pyenv:31] head -1
+ [pyenv:31] READLINK=/usr/bin/readlink
+ [pyenv:32] '[' -n /usr/bin/readlink ']'
+ [pyenv:53] '[' -z '' ']'
+ [pyenv:54] PYENV_ROOT=/Users/DS/.pyenv
+ [pyenv:58] export PYENV_ROOT
+ [pyenv:61] '[' -z '' ']'
+ [pyenv:62] '[' -n '' ']'
+ [pyenv:73] '[' -z '' ']'
+ [pyenv:74] PYENV_DIR=/Users/DS
+ [pyenv:80] export PYENV_DIR
+ [pyenv:83] shopt -s nullglob
++ [pyenv:85] abs_dirname /usr/local/Cellar/pyenv/20160202/libexec/pyenv
++ [pyenv:39] local cwd=/Users/DS
++ [pyenv:40] local path=/usr/local/Cellar/pyenv/20160202/libexec/pyenv
++ [pyenv:42] '[' -n /usr/local/Cellar/pyenv/20160202/libexec/pyenv ']'
++ [pyenv:43] cd /usr/local/Cellar/pyenv/20160202/libexec
++ [pyenv:44] local name=pyenv
+++ [pyenv:45] resolve_link pyenv
+++ [pyenv:35] /usr/bin/readlink pyenv
+++ [pyenv:45] true
++ [pyenv:45] path=
++ [pyenv:42] '[' -n '' ']'
++ [pyenv:48] pwd
++ [pyenv:49] cd /Users/DS
+ [pyenv:85] bin_path=/usr/local/Cellar/pyenv/20160202/libexec
+ [pyenv:89] export PATH=/usr/local/Cellar/pyenv/20160202/libexec:/usr/local/Cellar/pyenv-virtualenv/20160202/shims:/usr/local/Cellar/pyenv/20160202/libexec:/Users/DS/.pyenv/shims:/usr/local/bin:/usr/bin:/usr/sbin:/bin
+ [pyenv:89] PATH=/usr/local/Cellar/pyenv/20160202/libexec:/usr/local/Cellar/pyenv-virtualenv/20160202/shims:/usr/local/Cellar/pyenv/20160202/libexec:/Users/DS/.pyenv/shims:/usr/local/bin:/usr/bin:/usr/sbin:/bin
+ [pyenv:91] PYENV_HOOK_PATH=:/Users/DS/.pyenv/pyenv.d
+ [pyenv:92] '[' /usr/local/Cellar/pyenv/20160202 '!=' /Users/DS/.pyenv ']'
+ [pyenv:94] PYENV_HOOK_PATH=:/Users/DS/.pyenv/pyenv.d:/usr/local/Cellar/pyenv/20160202/pyenv.d
+ [pyenv:96] PYENV_HOOK_PATH=:/Users/DS/.pyenv/pyenv.d:/usr/local/Cellar/pyenv/20160202/pyenv.d:/usr/local/etc/pyenv.d:/etc/pyenv.d:/usr/lib/pyenv/hooks
+ [pyenv:100] export PYENV_HOOK_PATH
+ [pyenv:102] shopt -u nullglob
+ [pyenv:105] command=sh-activate
+ [pyenv:106] case "$command" in
++ [pyenv:119] command -v pyenv-sh-activate
+ [pyenv:119] command_path=/usr/local/bin/pyenv-sh-activate
+ [pyenv:120] '[' -n /usr/local/bin/pyenv-sh-activate ']'
+ [pyenv:122] shift 1
+ [pyenv:123] '[' --quiet = --help ']'
+ [pyenv:126] exec /usr/local/bin/pyenv-sh-activate --quiet
+ [pyenv-sh-activate:17] '[' -z /Users/DS/.pyenv ']'
+ [pyenv-sh-activate:25] unset FORCE
+ [pyenv-sh-activate:26] unset QUIET
+ [pyenv-sh-activate:27] unset VERBOSE
+ [pyenv-sh-activate:29] '[' 1 -gt 0 ']'
+ [pyenv-sh-activate:30] case "$1" in
+ [pyenv-sh-activate:44] QUIET=1
+ [pyenv-sh-activate:56] shift 1
+ [pyenv-sh-activate:29] '[' 0 -gt 0 ']'
+ [pyenv-sh-activate:59] no_shell=
+ [pyenv-sh-activate:60] versions=("$#")
+ [pyenv-sh-activate:61] '[' -z '' ']'
+ [pyenv-sh-activate:62] no_shell=1
+ [pyenv-sh-activate:63] OLDIFS='
'
+ [pyenv-sh-activate:64] IFS=:
+ [pyenv-sh-activate:64] versions=($(pyenv-version-name 2>/dev/null))
++ [pyenv-sh-activate:64] pyenv-version-name
+ [pyenv-sh-activate:65] IFS='
'
+ [pyenv-sh-activate:68] '[' -z 1 ']'
+ [pyenv-sh-activate:74] venv=system
+ [pyenv-sh-activate:76] '[' -n '' ']'
+ [pyenv-sh-activate:89] pyenv-virtualenv-prefix system
+ [pyenv-sh-activate:91] OLDIFS='
'
+ [pyenv-sh-activate:92] IFS=:
+ [pyenv-sh-activate:92] current_versions=($(pyenv-version-name))
++ [pyenv-sh-activate:92] pyenv-version-name
+ [pyenv-version-name:6] '[' -z '' ']'
++ [pyenv-version-name:7] pyenv-version-file
+ [pyenv-version-file:22] find_local_version_file /Users/DS
+ [pyenv-version-file:7] local root=/Users/DS
+ [pyenv-version-file:8] true
+ [pyenv-version-file:9] [[ /Users/DS =~ ^//[^/]*$ ]]
+ [pyenv-version-file:10] '[' -e /Users/DS/.python-version ']'
+ [pyenv-version-file:13] '[' -e /Users/DS/.pyenv-version ']'
+ [pyenv-version-file:17] '[' -n /Users/DS ']'
+ [pyenv-version-file:18] root=/Users
+ [pyenv-version-file:8] true
+ [pyenv-version-file:9] [[ /Users =~ ^//[^/]*$ ]]
+ [pyenv-version-file:10] '[' -e /Users/.python-version ']'
+ [pyenv-version-file:13] '[' -e /Users/.pyenv-version ']'
+ [pyenv-version-file:17] '[' -n /Users ']'
+ [pyenv-version-file:18] root=
+ [pyenv-version-file:8] true
+ [pyenv-version-file:9] [[ '' =~ ^//[^/]*$ ]]
+ [pyenv-version-file:10] '[' -e /.python-version ']'
+ [pyenv-version-file:13] '[' -e /.pyenv-version ']'
+ [pyenv-version-file:17] '[' -n '' ']'
+ [pyenv-version-file:17] break
+ [pyenv-version-file:23] '[' /Users/DS = /Users/DS ']'
+ [pyenv-version-file:25] global_version_file=/Users/DS/.pyenv/version
+ [pyenv-version-file:27] '[' -e /Users/DS/.pyenv/version ']'
+ [pyenv-version-file:29] '[' -e /Users/DS/.pyenv/global ']'
+ [pyenv-version-file:31] '[' -e /Users/DS/.pyenv/default ']'
+ [pyenv-version-file:34] echo /Users/DS/.pyenv/version
+ [pyenv-version-name:7] PYENV_VERSION_FILE=/Users/DS/.pyenv/version
++ [pyenv-version-name:8] pyenv-version-file-read /Users/DS/.pyenv/version
+ [pyenv-version-file-read:6] VERSION_FILE=/Users/DS/.pyenv/version
+ [pyenv-version-file-read:8] '[' -e /Users/DS/.pyenv/version ']'
+ [pyenv-version-file-read:22] exit 1
++ [pyenv-version-name:8] true
+ [pyenv-version-name:8] PYENV_VERSION=
+ [pyenv-version-name:11] '[' -z '' ']'
+ [pyenv-version-name:12] echo system
+ [pyenv-version-name:13] exit
+ [pyenv-sh-activate:93] IFS='
'
+ [pyenv-sh-activate:94] new_venv=system/envs/system
+ [pyenv-sh-activate:95] pyenv-virtualenv-prefix system/envs/system
+ [pyenv-sh-activate:99] '[' -z 1 ']'
+ [pyenv-sh-activate:102] echo false
+ [pyenv-sh-activate:103] exit 1
+_pyenv_virtualenv_hook:5> true
+_pyenv_virtualenv_hook:5> eval false
+(eval):1> false
+_pyenv_virtualenv_hook:5> true
+_pyenv_virtualenv_hook:7> return 0
+omz_termsupport_precmd:1> emulate -L zsh
+omz_termsupport_precmd:3> [[ '' == true ]]
+omz_termsupport_precmd:7> title '%15<..<%~%<<' %n#%m
+title:1> emulate -L zsh
+title:2> setopt prompt_subst
+title:4> [[ '' == *term* ]]
+title:8> : %n#%m
+title:10> case ansi (cygwin | xterm* | putty* | rxvt* | ansi)
+title:12> print -Pn '\e]2;%n#%m\a'
+title:13> print -Pn '\e]1;%15\<..\<%~%\<\<\a'
+update_terminalapp_cwd:1> emulate -L zsh
+update_terminalapp_cwd:4> omz_urlencode -P /Users/DS
+omz_urlencode:1> emulate -L zsh
+omz_urlencode:2> zparseopts -D -E -a opts r m P
+omz_urlencode:4> local 'in_str=/Users/DS'
+omz_urlencode:5> local 'url_str='
+omz_urlencode:6> local spaces_as_plus
+omz_urlencode:7> [[ -z -P ]]
+omz_urlencode:8> local 'str=/Users/DS'
+omz_urlencode:11> local 'encoding=UTF-8'
+omz_urlencode:12> local safe_encodings
+omz_urlencode:13> safe_encodings=( UTF-8 utf8 US-ASCII )
+omz_urlencode:14> [[ -z UTF-8 ]]
+omz_urlencode:23> local i byte ord 'LC_ALL=C'
+omz_urlencode:24> export LC_ALL
+omz_urlencode:25> local 'reserved=;/?:#&=+$,'
+omz_urlencode:26> local 'mark=_.!~*()-'
+omz_urlencode:27> local 'dont_escape=[A-Za-z0-9'
+omz_urlencode:28> [[ -z '' ]]
+omz_urlencode:29> dont_escape+=';/?:#&=+$,'
+omz_urlencode:32> [[ -z '' ]]
+omz_urlencode:33> dont_escape+='_.!~*()-'
+omz_urlencode:35> dont_escape+=']'
+omz_urlencode:39> local 'url_str='
+omz_urlencode:40> i = 1
+omz_urlencode:40> i <= 9
+omz_urlencode:41> byte=/
+omz_urlencode:42> [[ "$byte" -regex-match "$dont_escape" ]]
+omz_urlencode:43> url_str+=/
+omz_urlencode:40> ++i
+omz_urlencode:40> i <= 9
+omz_urlencode:41> byte=U
+omz_urlencode:42> [[ "$byte" -regex-match "$dont_escape" ]]
+omz_urlencode:43> url_str+=U
+omz_urlencode:40> ++i
+omz_urlencode:40> i <= 9
+omz_urlencode:41> byte=s
+omz_urlencode:42> [[ "$byte" -regex-match "$dont_escape" ]]
+omz_urlencode:43> url_str+=s
+omz_urlencode:40> ++i
+omz_urlencode:40> i <= 9
+omz_urlencode:41> byte=e
+omz_urlencode:42> [[ "$byte" -regex-match "$dont_escape" ]]
+omz_urlencode:43> url_str+=e
+omz_urlencode:40> ++i
+omz_urlencode:40> i <= 9
+omz_urlencode:41> byte=r
+omz_urlencode:42> [[ "$byte" -regex-match "$dont_escape" ]]
+omz_urlencode:43> url_str+=r
+omz_urlencode:40> ++i
+omz_urlencode:40> i <= 9
+omz_urlencode:41> byte=s
+omz_urlencode:42> [[ "$byte" -regex-match "$dont_escape" ]]
+omz_urlencode:43> url_str+=s
+omz_urlencode:40> ++i
+omz_urlencode:40> i <= 9
+omz_urlencode:41> byte=/
+omz_urlencode:42> [[ "$byte" -regex-match "$dont_escape" ]]
+omz_urlencode:43> url_str+=/
+omz_urlencode:40> ++i
+omz_urlencode:40> i <= 9
+omz_urlencode:41> byte=D
+omz_urlencode:42> [[ "$byte" -regex-match "$dont_escape" ]]
+omz_urlencode:43> url_str+=D
+omz_urlencode:40> ++i
+omz_urlencode:40> i <= 9
+omz_urlencode:41> byte=S
+omz_urlencode:42> [[ "$byte" -regex-match "$dont_escape" ]]
+omz_urlencode:43> url_str+=S
+omz_urlencode:40> ++i
+omz_urlencode:40> i <= 9
+omz_urlencode:53> echo -E /Users/DS
+update_terminalapp_cwd:4> local 'URL_PATH=/Users/DS'
+update_terminalapp_cwd:5> [[ 0 != 0 ]]
+update_terminalapp_cwd:8> printf '\e]7;%s\a' file://YuDaesungui-MacBook-Pro.local/Users/DS
+-zsh:5> build_prompt
+build_prompt:1> RETVAL=0
+build_prompt:2> prompt_status
+prompt_status:1> local symbols
+prompt_status:2> symbols=( )
+prompt_status:3> [[ 0 -ne 0 ]]
+prompt_status:4> [[ 501 -eq 0 ]]
+prompt_status:5> [[+prompt_status:5> jobs -l
+prompt_status:5> [[+prompt_status:5> wc -l
+prompt_status:5> [[ ' 0' -gt 0 ]]
+prompt_status:7> [[ -n '' ]]
+build_prompt:3> prompt_virtualenv
+prompt_virtualenv:1> local 'virtualenv_path='
+prompt_virtualenv:2> [[ -n '' ]]
+build_prompt:4> prompt_context
+prompt_context:1> [[ DS != ]]
+prompt_context:2> prompt_segment black default '%(!.%{%F{yellow}%}.)DS#%m'
+prompt_segment:1> local bg fg
+prompt_segment:2> [[ -n black ]]
+prompt_segment:2> bg='%K{black}'
+prompt_segment:3> [[ -n default ]]
+prompt_segment:3> fg='%F{default}'
+prompt_segment:4> [[ NONE != NONE ]]
+prompt_segment:7> echo -n '%{%K{black}%}%{%F{default}%} '
+prompt_segment:9> CURRENT_BG=black
+prompt_segment:10> [[ -n '%(!.%{%F{yellow}%}.)DS#%m' ]]
+prompt_segment:10> echo -n '%(!.%{%F{yellow}%}.)DS#%m'
+build_prompt:5> prompt_dir
+prompt_dir:1> prompt_segment blue black '%~'
+prompt_segment:1> local bg fg
+prompt_segment:2> [[ -n blue ]]
+prompt_segment:2> bg='%K{blue}'
+prompt_segment:3> [[ -n black ]]
+prompt_segment:3> fg='%F{black}'
+prompt_segment:4> [[ black != NONE && blue != black ]]
+prompt_segment:5> echo -n ' %{%K{blue}%F{black}%}%{%F{black}%} '
+prompt_segment:9> CURRENT_BG=blue
+prompt_segment:10> [[ -n '%~' ]]
+prompt_segment:10> echo -n '%~'
+build_prompt:6> prompt_git
+prompt_git:1> (( 1 ))
+prompt_git:2> local PL_BRANCH_CHAR
+prompt_git:3> '(anon)'
+(anon):1> local 'LC_ALL=' 'LC_CTYPE=en_US.UTF-8'
+(anon):2> PL_BRANCH_CHAR=
+prompt_git:7> local ref dirty mode repo_path
+prompt_git:8> repo_path=+prompt_git:8> git rev-parse --git-dir
+prompt_git:8> repo_path=''
+prompt_git:10> git rev-parse --is-inside-work-tree
+build_prompt:7> prompt_bzr
+prompt_bzr:1> (( 0 ))
+prompt_bzr:1> return
+build_prompt:8> prompt_hg
+prompt_hg:1> (( 0 ))
+prompt_hg:1> return
+build_prompt:9> prompt_end
+prompt_end:1> [[ -n blue ]]
+prompt_end:2> echo -n ' %{%k%F{blue}%}'
+prompt_end:6> echo -n '%{%f%}'
+prompt_end:7> CURRENT_BG='

The problem was kaspersky program :(
I did not matter with pyenv and zsh

Related

Set environment variable with cron. Not working

How can I set for example with cronjob
1/* * * * * bash -c 'export TZ=Europe/London'
1/* * * * * bash -c '. /path/to/script.sh'
This script has multiple exports which is getting the timezone offset and would like that cronjob update the system var TZ with that one.
If I run it from shell it works with
". /path/to/script.th" as source but not in a cronjob.
At least how to set the environment variables with cronjob.
I also have tried that the script writes into the /etc/environment
TZ=Europe/London
and cronjob to set -a;. /etc/environment; set +a;
None are working but they do from shell.
Here is my script. What are my options from here when the script is ran by cronjob or anything else...Also tried to make it run with supervisord...same result.
Everything is working running locally from shell, but can't figure out how to modify the system "$TZ" environment with a script or from cronjob.
The script writes into the /etc/environment but when I set it from cron still won't update but it will from shell.
I know it all works from current shell but /etc/environment should be available to all.
I have edited so the way to go is write with the script into the /etc/profile.d/custom.sh
This works also inside a docker container to update periodicaly environmental variable if needed.
#!/bin/bash
#######################
## Preload variables ##
#######################
. /etc/profile
. ~/.bash_profile
. ~/.bashrc
#############################
## Get Boardtime from API ##
#############################
boardtime=$(curl -s http://server/api/get-time \
-u user:pass \
-d locale='en_US' | \
grep -oP '(?<=boardTime":")[^","boardTimeLocalized"]*' | \
sed -r -e 's/^.{8}/& /' -e 's/[^ ]{2}/&:/5g' | \
sed 's/:$//')
formatted_boardtime=$(date -d "$boardtime" "+%a, %e %b %Y %H:%M:%S %z")
echo -ne "\nLocaltime is $formatted_boardtime!\n"
###################################
## Get timezone offset from ship ##
###################################
minutes=$(curl -s http://server/timezone/currentOffset.txt | grep -oP "[-+][0-9]{0,9}/|[0-9]{0,9}")
echo -ne "\nTimezone offset is $minutes\n"
((h=$minutes / 60))
#############################
## Set Offset Symbol Value ##
#############################
symbol=$(echo $minutes | grep -oP "[-]")
digit=$(echo $h | grep -oP "[0-9]{2,4}")
if [[ $symbol == "-" ]] && [[ $digit ]]; then
offset="-"$h"00"
elif [[ $symbol != "-" ]] && [[ $digit ]]; then
offset="+"$h"00"
elif [[ $symbol == "-" ]] && [[ ! $digit ]]; then
offset="-0$(echo "$h" | grep -oP '[0-9]{0,4}')00"
else
offset="+0$(echo "$h" | grep -oP '[0-9]{0,4}')00"
fi
##########################
## Set Current timezone ##
##########################
localtime=$(echo $formatted_boardtime | sed -E "s/([-+][0-9]{4})$/"$offset"/")
if [[ $symbol == "-" && $offset == -1100 ]]; then
export TZ="US/Samoa"
elif [[ $symbol == "-" && $offset == -1000 ]]; then
export TZ="Pacific/Honolulu"
elif [[ $symbol == "-" && $offset == -0900 ]]; then
export TZ="America/Nome"
elif [[ $symbol == "-" && $offset == -0800 ]]; then
export TZ="America/Los_Angeles"
elif [[ $symbol == "-" && $offset == -0700 ]]; then
export TZ="America/Denver"
elif [[ $symbol == "-" && $offset == -0600 ]]; then
export TZ="America/Cancun"
elif [[ $symbol == "-" && $offset == -0500 ]]; then
export TZ="America/Lima"
elif [[ $symbol == "-" && $offset == -0400 ]]; then
export TZ="America/Santiago"
elif [[ $symbol == "-" && $offset == -0300 ]]; then
export TZ="America/Bahia"
elif [[ $symbol == "-" && $offset == -0200 ]]; then
export TZ="America/Noronha"
elif [[ $symbol == "-" && $offset == -0100 ]]; then
export TZ="Atlantic/Azores"
elif [[ $symbol != "-" && $offset == +0000 ]]; then
export TZ="Europe/London"
elif [[ $symbol != "-" && $offset == +0100 ]]; then
export TZ="Europe/Berlin"
elif [[ $symbol != "-" && $offset == +0200 ]]; then
export TZ="Europe/Athens"
elif [[ $symbol != "-" && $offset == +0300 ]]; then
export TZ="Asia/Qatar"
elif [[ $symbol != "-" && $offset == +0400 ]]; then
export TZ="Asia/Dubai"
elif [[ $symbol != "-" && $offset == +0500 ]]; then
export TZ="Indian/Maldives"
elif [[ $symbol != "-" && $offset == +0600 ]]; then
export TZ="Asia/Thimbu"
elif [[ $symbol != "-" && $offset == +0700 ]]; then
export TZ="Asia/Bangkok"
elif [[ $symbol != "-" && $offset == +0800 ]]; then
export TZ="Asia/Hong_Kong"
elif [[ $symbol != "-" && $offset == +0900 ]]; then
export TZ="Asia/Tokyo"
elif [[ $symbol != "-" && $offset == +1000 ]]; then
export TZ="Australia/Melbourne"
elif [[ $symbol != "-" && $offset == +1100 ]]; then
export TZ="Pacific/Ponape"
elif [[ $symbol != "-" && $offset == +1200 ]]; then
export TZ="Pacific/Fiji"
elif [[ $symbol != "-" && $offset == +1300 ]]; then
export TZ="Pacific/Apia"
elif [[ $symbol != "-" && $offset == +1400 ]]; then
export TZ="Pacific/Kiritimati"
fi
echo -ne "\nLocaltime is $localtime!\n"
echo -ne "\nTimezone matching offset is set $TZ!\n"
echo "$TZ" > /etc/timezone
echo "TZ=$TZ" > /etc/profile.d/custom.sh
###########################################################
## Restart postfix to apply new timezone also to postfix ##
###########################################################
/usr/sbin/postfix stop && /usr/sbin/postfix start
Can mark as a working solution for this using as follows then:
The solution is create a /etc/profile.d/custom.sh
Write in there you environment variable or your script can write in there and create the file with the content you need.
Inside there simply add all the environments you need like
export TZ=Europe/London
export MYENV=value
export etc..
..

How to do double-indirection with jq variables?

How to construct a name of a variable from data and access that variable?
For example it should somehow give the content of the alpha-file:
jq '$.var"-file"' --slurpfile alpha-file <(echo 0) --slurpfile beta-file <(echo 1) <<<'{"var": "alpha"}'
It should output:
[
0
]
Named arguments are also available to the jq program as $ARGS.named.
So there is a dictionary to extract variables by a string name from:
jq '$ARGS.named["\(.var)-file"]' --slurpfile alpha-file <(echo 0) --slurpfile beta-file <(echo 1) <<<'{"var": "alpha"}'
outputs
[
0
]
You could use getpath like this:
jq --slurpfile alphaFile <(echo 0) --slurpfile betaFile <(echo 1) '
.var as $ab | {alpha: $alphaFile, beta: $betaFile} | getpath([$ab])' <<< '{"var": "alpha"}'
Better yet:
jq --slurpfile alpha <(echo 0) --slurpfile beta <(echo 1) '
.var as $ab | {$alpha, $beta} | getpath([$ab])' <<< '{"var": "alpha"}'

result not showing when looping

I'm doing a powershell which runs through a loop to get result from an sql query with different parameters. the issue is only the first run of loop has a result. the rest all blank result which should also show some result.
$Days = #(90,60,30,15)
$Start = 0
$End = 0
$Affiliation = 1
for ($i = 0 ; $i -lt $Affiliation.Count ; $i++) {
for ($i = 0; $i -lt $Days.Count ; $i++){
if ($Days[$i] -eq 1) {
$End = $Days[$i]
$Start = 0
write-host "Start/End r:" $Start $End
}
elseIf ($index -eq $Days.Length) {
$End = $Days[$i]
$Start = 0
write-host "Start/End m:" $Start $End
}
else {
$End = $Days[$i]
$Start = $Days[$i+1] + 1
write-host "Start/End s:" $Start $End
}
########## Queries
$Querydays = " SELECT
distinct tblcustomer.CustomerName AS Customer_Name
, tblsma.sorno AS SMA_SOR_Number
, FUNC_GET_JSON_VALUES('Name', tblsma.products) AS Products
, DATE_FORMAT(tblsma.expiryDate, '%m/%d/%Y') AS Expiry_Date
, tblsma.Remarks AS 'Remarks'
, DATEDIFF( DATE(tblsma.expiryDate), CURDATE()) AS Days_Left_Before_Expiration
#,GROUP_CONCAT(tblsma.products) AS 'PRODUCT'
FROM
where
tblcustomer.affiliationID = '" + $Affiliation[$i] + "'
and tblcustomer.affiliationID <> 95
and tblaccountsgroup.GroupID <> 5
and datediff(DATE(tblsma.expiryDate), CURDATE()) between " + $Start + " and " + $End + "
order by Days_Left_Before_Expiration DESC"
#####################
$ResultDays=$null
$ResultDays = MysqlConn -Query $Querydays
Write-Host $ResultDays
$EmailBody = $EmailBody + "`r `n" + "<b>List of Valid License that will due in next " + $Days[$i] + " days:</b>"
foreach($row in $ResultDays)
{
$EmailBody = "`n" + $EmailBody + "`r `n" + "Account Name : " + $row.Item(0) + "`r" +
"SOR No. : " + $row.Item(1) + "`r" +
"Product Name : " + $row.Item(2) + "`r" +
"Expiry Date : " + $row.Item(3) + "`r" +
"Remarks : " + $row.Item(4) + "`r" +
"Days Before Expiry : " + $row.Item(5) + "`r" +
"`n"
}
$rep = $Days[$i+1]
}
}
Start/End s: 61 90
System.Data.DataRow System.Data.DataRow System.Data.DataRow
Start/End s: 31 60
Start/End s: 16 30
Start/End s: 1 15
Because you use in both forloops the same variable $i this will be overwritten all the time. Change the name of one of them.
Bad:
for ($i = 1; $i -lt 11; $i ++)
{
('First for loop: ' + $i)
for ($i = 1; $i -lt 11; $i ++)
{
('Second for loop: ' + $i)
}
}
First for loop: 1
Second for loop: 1
Second for loop: 2
Second for loop: 3
Second for loop: 4
Second for loop: 5
Second for loop: 6
Second for loop: 7
Second for loop: 8
Second for loop: 9
Second for loop: 10
Good:
for ($i = 1; $i -lt 11; $i ++)
{
('First for loop: ' + $i)
for ($x = 1; $x -lt 11; $x ++)
{
('Second for loop: ' + $x)
}
}
First for loop: 1
Second for loop: 1
Second for loop: 2
Second for loop: 3
Second for loop: 4
Second for loop: 5
Second for loop: 6
Second for loop: 7
Second for loop: 8
Second for loop: 9
Second for loop: 10
First for loop: 2
Second for loop: 1
Second for loop: 2
...

Bourne Shell Conditional Operators

I am having a lot of fun playing with Bourne Shell, but I am facing a quite cryptic situation regarding conditions:
#! /bin/sh
a=1
b=2
c="0 kB/s"
if [ "$a" -eq 1 ] ; then echo "a = 1: true" ; else echo "a = 1: false" ; fi
if [ "$b" -gt 0 ] ; then echo "b > 0: true" ; else echo "b > 0: false" ; fi
if [ "$c" != "0 kB/s" ] ; then echo "c <> 0: true" ; else echo "c <> 0: false" ; fi
if [ "$a" -eq 1 ] || [ "$b" -gt 0 ] ; then echo "a = 1 or b > 0: true" ; else echo "a = 1 or b > 0: false" ; fi
if [ "$a" -eq 1 ] || [ "$b" -gt 0 ] && [ "$c" != "0 kB/s" ] ; then echo "a = 1 or b > 0 and c <> 0: true" ; else echo "a = 1 or b > 0 and c <> 0: false" ; fi
if [ true ] || [ true ] && [ false ] ; then echo "true or true and false: true" ; else echo "true or true and false: false" ; fi
gives me the following result:
a = 1: true
b > 0: true
c <> 0: false
a = 1 or b > 0: true
a = 1 or b > 0 and c <> 0: false
true or true and false: true
Short question: why don't I get a = 1 or b > 0 and c <> 0: true?
Thanks a lot for your help ...
|| and && have equal precedence, unlike in languages where the logical AND operator binds more tightly than the logical OR. This means your code as written is equivalent to
if { [ "$a" -eq 1 ] || [ "$b" -gt 0 ]; } && [ "$c" != "0 kB/s" ] ; then
echo "a = 1 or b > 0 and c <> 0: true"
else
echo "a = 1 or b > 0 and c <> 0: false"
fi
rather than the expected
if [ "$a" -eq 1 ] || { [ "$b" -gt 0 ] && [ "$c" != "0 kB/s" ]; } ; then
echo "a = 1 or b > 0 and c <> 0: true"
else
echo "a = 1 or b > 0 and c <> 0: false"
fi

Repeat printf arguments with command line operators

I want to repeat the same argument $i for the instances 03-12. I'm really trying to use some nco operators - but the printf statement is hanging me up.
I'm trying to use an netcdf operator on it - where these outputs of the printf are the input files to the command. While this works now with the printf statements, it's not piping into the netcdf command. Which goes as: ncea -v T,U inputfiles outputfile
#!/bin/csh
set i = 1
while ($i < 2)
ncea -v T,U
foreach j ( {3,4,6,7,8,9,10,11,12} )
`printf O3_BDBP_1979ghg.cam.h0.00%02d-%02d.nc $j $i `
end
O3_BDBP_1979.nc
# i = $i + 1
end
Other printf statements I've tried are
ncea -v T,U `printf O3_BDBP_1979ghg.cam.h0.00{03,04,05,06,07,08,09,10,11,12}-%02d.nc $i` O3_BDBP_1979.nc
ncea -v T,U `printf O3_BDBP_1979ghg.cam.h0.00{03,04,05,06,07,08,09,10,11,12}-%1$02d.nc $i` O3_BDBP_1979.nc