Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

Incrementing in Bash using an Until Loop

i=0

until [ $i -gt 3 ]
do
  echo i: $i
  ((i=i+1))
done
Comment

bash increment variable in while loop

You are using USCOUNTER in a subshell, that's why the variable is not showing in the main shell.

Instead of cat FILE | while ..., do just a while ... done < $FILE. This way, you avoid the common problem of I set variables in a loop that's in a pipeline. Why do they disappear after the loop terminates? Or, why can't I pipe data to read?:

while read country _; do
  if [ "US" = "$country" ]; then
        USCOUNTER=$(expr $USCOUNTER + 1)
        echo "US counter $USCOUNTER"
  fi
done < "$FILE"
Note I also replaced the `` expression with a $().

I also replaced while read line; do country=$(echo "$line" | cut -d' ' -f1) with while read country _. This allows you to say while read var1 var2 ... varN where var1 contains the first word in the line, $var2 and so on, until $varN containing the remaining content.
Comment

PREVIOUS NEXT
Code Example
Shell :: yacc install in kali linux 
Shell :: recover a merged commit git 
Shell :: revert to commit git 
Shell :: stash specific files git 
Shell :: install go on mac brew 
Shell :: Why double tapping icon doesnt minimize in ubuntu 
Shell :: powershell get disk space remote computer 
Shell :: git stash in file 
Shell :: windows shell 
Shell :: i get your branch is ahead of master after I pulled from remote master 
Shell :: realtek 
Shell :: bash code for loop 
Shell :: sed replace 
Shell :: linux os upgrade 
Shell :: gcloud add role to service account 
Shell :: turtlebot installation 
Shell :: tricks to do with ubuntu 
Shell :: aws create repository cli 
Shell :: rm -rf except 
Shell :: docker secrets commands 
Shell :: grep remove -- 
Shell :: git merge without delete files 
Shell :: pwa install 
Shell :: linux add icon to .desktop file 
Shell :: print next 10 lines after grep 
Shell :: bash manual 
Shell :: react native cannot connect to metro server 
Shell :: to create repo from cmd 
Shell :: git stash bitbucket 
Shell :: swagger editor locally 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =