shell - To pick certain entries from a file in unix -


i have text file 4 entries. when run shell script first time, first entry should picked . when run 2nd time, 2nd entry should picked , when run script 5th time again 1st entry should picked up.

basically should pick entries in text file 1 one , when reaches last , again should start first.

i thought of keeping 1 more file , current running entry , when runs next time check file , process next line. cannot keep files. logic should handled inside script without keeping other files.

i hope explained problem.. please me logic in unix ..

you can use trap in brute force call sed update index within file on termination or exit. example:

#!/bin/bash  declare -i current_idx=0  trap 'sed -i "s/^declare[ ]-i[ ]current_idx[=].*$/declare -i \ current_idx=$(((current_idx+1) % 4))/" $(readlink -f "$0")' sigterm exit  case "$current_idx" in     0 ) printf "picked %d entry\n" "$current_idx";;     1 ) printf "picked %d entry\n" "$current_idx";;     2 ) printf "picked %d entry\n" "$current_idx";;     3 ) printf "picked %d entry\n" "$current_idx";;     * ) printf "error: current_idx '%d'\n" "$current_idx";; esac 

example use

here script called 11 times showing how cycles through 4 different entries returning first on 5th call.

$ (for in {0..10}; bash selfmod.sh; done) picked 0 entry picked 1 entry picked 2 entry picked 3 entry picked 0 entry picked 1 entry picked 2 entry picked 3 entry picked 0 entry picked 1 entry picked 2 entry 

note: self-modifying script isn't practice. separate-file idea safer. however, purposes, work. however, make no representation how wise or safe is.


Comments

Popular posts from this blog

Load Balancing in Bluemix using custom domain and DNS SRV records -

oracle - pls-00402 alias required in select list of cursor to avoid duplicate column names -

python - Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] error -