how to find the value of a variable in zsh?

I have a zsh script written by someone else, I am trying to modify it to work on slightly different data -problem is I know nothing about shell scripting.
I am trying to muddle through this myself since I need to learn but can someone tell me how to debug a script? ie. I want to display the value of a variable and pause the script. Is this possible? If so how?

many thanks.
:wall:cmp

As in many other languages, there are several ways you can debug a shell script.

To display the value you can use "echo" or "printf" commands.

Pause execution use "sleep".

Stop at a specified place, use "exit".

To see how to use these commands, type "help <command>".

1 Like

thats great, thanks.
I am also unsure what the first line of the script does, can you tell me?

while [ "$#" -gt 0 ]

I know it basicy means while -while greater than zero- but what does the first part "$# do?

thanks
cmp

Number of arguments passed to the script, your condition means that if at least one argument is passed to the script, it may go for a indefinite while loop, which acts as pause/something.

1 Like

I know this is a but brassy but could someone comment this so I can learn what is going on? It has some comment but not enough for me to understand. the script(s) operate on a series of directories named as a singlle date, eachdirectory contains a file logfile.txt containing timestamped data
many thanks

its actually 2 seperate scripts, the first one feeds data to the second one:
(in zsh for DOS)
first script

# because windows CMD doesn't actually expand * into arguments for you
exec ./zsh.exe batmon.zsh 20[0-9][0-9]-* > events.txt 2> stats.txt

2nd script

#!/bin/zsh

# Loop over all commandline parameters 2011-*
# basename strips path just leaving name of file
while [ "$#" -gt 0 ]
do
        DAY=$(./basename.exe "$1" )
        DAY="${DAY[0,4]},${DAY[6,7]},${DAY[9,10]}"

    # The datafiles contain \r, which must be stripped somehow
        while read LINE
    do
        # Convert HH:MM:SS into HH,MM,SS to make awk easier
                TIME="${LINE[5,12]}"
                TIME="${TIME[0,2]},${TIME[4,5]},${TIME[7,8]}"
                echo -e "${DAY},${LINE[0,2]},${TIME},${LINE[14,99]}\r"
    done < "$1/LogFile.txt"
    shift
done > output.txt
./gawk.exe -F "," -f batmon.awk < output.txt

scripts try to guess what they are doing this..
first script calls the second script (batmon.zsh)
and batmon.zsh exec executes in first script with exec (you can think so batmon script replaced in first script and executes batmon.zsh with 20[0-9][0-9]-* parameter --> * expands to pattern match )

exec zsh batmon.zsh 20[0-9][0-9]-* > events.txt 2> stats.txt

as result batmon.zsh is running with "20[0-9][0-9]-*"

 
* while [ "$#" -gt 0 ] --> if argument count is greater than 0 then loop executes..so if there is an argument then go on in while loop..
so if there is any (folder) file matches pattern with "20[0-9][0-9]-*" in local dir in where script executes , then loop runs.
 
# ls -ld 20[0-9][0-9]-*
drwxr-xr-x 2 root root 4096 2011-05-20 15:16 2011-05-15 20:10:10
drwxr-xr-x 2 root root 4096 2011-05-20 14:47 2011-05-18
drwxr-xr-x 3 root root 4096 2011-05-20 15:35 2011-05-20
 
so our second script executes like this--> # ./batmon.zsh "2011-05-15 20:10:10" 2011-05-18 2011-05-20 

* ./basename.exe "$1" --> in this state , for examle our argument gets "2011-05-20" "2011-05-18" and "2011-05-15 18:00:00" if there are in there.
if you pattern like this , basename is unnecassary but i dont know about the format or i dont know what script owner intended for this.
however if our parameter "2011-20-25/20:00:00" after basename then our variable is "2011-20-25"..
 
* DAY="${DAY[0,4]},${DAY[6,7]},${DAY[9,10]}" --> this gets 0-4 and 6-7 and 9-10 chars with separated a comma.
 
* while read LINE ; do .... ; done <"$1/LogFile.txt" --> if our first argument  "2011-05-20" , 
it looks LogFile.txt in first argument dir then redirects to stdinput to LogFile.txt for while statement so all readings happens from LogFile.txt.
 
# find -type d -name "20[0-9][0-9]-*" |xargs -d'\n' ls -1
./2011-05-15 20:10:10:
LogFile.txt
./2011-05-18:
LogFile.txt
./2011-05-20:
LogFile.txt
 
# more LogFile.txt  ( all contents are completely predict )
Thu 20:10:10:0000000000000000345
Sun 22:10:10:0000000000000125651
 
 * TIME="${LINE[5,12]}"
   TIME="${TIME[0,2]},${TIME[4,5]},${TIME[7,8]}"
   echo -e "${DAY},${LINE[0,2]},${TIME},${LINE[14,99]}\r"
in this, we read a line from LogFile.txt and split format to like above and write to output.txt with 
`done > output.txt` in last line of the first while.
 
* for example if we think to read the line below
Thu 20:10:10:0000000000000000345  
   TIME="${LINE[5,12]}"  --> 20:10:10
   TIME="${TIME[0,2]},${TIME[4,5]},${TIME[7,8]}"  --> 20,10,10
   echo -e "${DAY},${LINE[0,2]},${TIME},${LINE[14,99]}\r" --> 2011-05-20,Th,20,10,10,0000000000000000345
 
* shift  --> if our arguments (parameters) are ["2011-05-15 20:10:10" 2011-05-18 2011-05-20 ] . after shift then $1 --> 2011-05-18
another shift then $1 --> 2011-05-20 and then...so it shifts the parameters to left.
 
* ./gawk.exe -F "," -f batmon.awk < output.txt --> extract lines according to batmon.awk from output.txt 

regards
ygemici

1 Like

I used MS Access query to transform the 1st block of data below so it looks like the second block of data.
Is there any whay to do this using AWK or shell script?

808,634380820948829889,10/4/2011 01:28:15,"192.168.0.9",1,0
809,634380820950038523,10/4/2011 01:28:15,"192.168.0.9",0,0
810,634380829060872853,11/4/2011 01:41:46,"192.168.0.9",0,1
811,634380829062172319,11/4/2011 01:41:46,"192.168.0.9",1,1

I am trying to alter the above data so it reads similar to this:

2011,04,10,rx,01,28,15,pv,1,0
2011,04,10,rx,01,28,15,pv,0,0
2011,04,10,rx,01,41,46,pv,0,1
2011,04,10,rx,01,41,46,pv,1,1
:o
thanks