how to find the value of a variable in zsh?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to find the value of a variable in zsh?
# 1  
Old 05-19-2011
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.
Smiliecmp
# 2  
Old 05-19-2011
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>".
This User Gave Thanks to Shell_Life For This Post:
# 3  
Old 05-20-2011
another question on zsh script

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
# 4  
Old 05-20-2011
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.
This User Gave Thanks to kumaran_5555 For This Post:
# 5  
Old 05-20-2011
zsh scripting, please help me learn

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
Code:
# 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

Code:
#!/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


Last edited by Franklin52; 05-20-2011 at 11:28 AM.. Reason: Please use code tags
# 6  
Old 05-20-2011
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 )
Code:
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]-*"

Code:
 
* 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
This User Gave Thanks to ygemici For This Post:
# 7  
Old 05-20-2011
perhaps easier if I explain what I am tring to do

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
Smilie
thanks

Last edited by cmp260; 05-25-2011 at 01:13 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Zsh array -a vs. -A question

Inside a zsh function, I create a local array with local -a arrayname and a local associative array with local -A arrayname. I also can create an array using set, like this: set -A arrayname value1 value2 value3In this form, I can not explicitly declare that an array is associative or... (2 Replies)
Discussion started by: rovf
2 Replies

2. Shell Programming and Scripting

Can I do this without eval? (zsh)

I have in one shell variable, op, a string which represents a "test operator" in a ] construct, for instance -d or -n or -s, an in another shell variable, arg, some arbitrary string. What I want to achieve, is basically this: #This is INCORRECT code. I just want to get you the idea, what I'm... (7 Replies)
Discussion started by: rovf
7 Replies

3. UNIX for Dummies Questions & Answers

ZSH Colors on AIX7

I'd put together a zshrc for use on Linux & AIX 5/6 at another shop that worked fine, but when I tried to put it on here it messes up the colors for some reason I can't understand. I used Phil's ZSH prompt for starters, and everything worked before, and still does on Linux, but isn't working on... (0 Replies)
Discussion started by: Vryali
0 Replies

4. Shell Programming and Scripting

zsh, prompt, variable expansion, ANSI color sequences

Hi! I am using latest ZSH and I have setopt prompt_subst I have a global hash array variable which contains some color definitions with color names as keys and 256-color ANSI codes (with %{ and %}) as values, eg %{\ePS1="$FG Hello World (%M) > " or PS1=$'$FG Hello World (%M) > ' then it... (3 Replies)
Discussion started by: galanom
3 Replies

5. Shell Programming and Scripting

zsh and host completion

Hi there is there a way i can add to my .zshrc so that when i type rsh <tab> it takes the name for a list of hosts i know it looks in .ssh/know_hosts but i want it for rsh and for a list that i supply thanks A (4 Replies)
Discussion started by: ab52
4 Replies

6. Shell Programming and Scripting

Using zsh

Hi all i am forced to use tcsh at work but i want to use zsh, so i have added this to my .cshrc if (! $?STARTTCSH) then if ("$tty" != "" && -x /bin/zsh) exec /bin/zsh exit endif but this now stopped me going back to tcsh if i need to, is there a way to do this, i would... (7 Replies)
Discussion started by: ab52
7 Replies

7. UNIX for Dummies Questions & Answers

question about zsh

hi, In bash, $ bind -P | grep yank-last yank-last-arg can be found on "\M-.", "\M-_". this allows me to press ALT key and the period (.) to yank the last argument of the previous command line into the current command line. How can I get the same behavior in zsh ? Thanks ... (0 Replies)
Discussion started by: Andrewkl
0 Replies

8. UNIX for Dummies Questions & Answers

Z-shell (zsh)

Z-shell (zsh) anyone use it and how do ya like it? (1 Reply)
Discussion started by: Bodhi
1 Replies

9. Shell Programming and Scripting

tutorials about zsh

hi there I'm looking for tutorials about zsh (beginners to experts) can you give me addresses please? thx a lot (3 Replies)
Discussion started by: SpY974
3 Replies
Login or Register to Ask a Question

Featured Tech Videos