Sponsored Content
Top Forums Shell Programming and Scripting Getting error on for loop - bash script Post 302513778 by arundhati_s on Thursday 14th of April 2011 04:42:31 AM
Old 04-14-2011
Getting error on for loop - bash script

Hi,
I am working on bash script after a long time. I am getting error near done statement while running a for loop snippet. The error says "Syntax error near unexpcted token 'done'"

please suggest what could be wrong. here is the snippet

Code:
elements=${#option_arr[@]} //an array of values
for((i=0;i<$elements;i++)); do
 if [ "${i}" -eq 0 ]; then
         BuySell="Buy"
        else if [ "${i}" -gt 0 ]; then
          if ["$BuySell" -eq "Buy" ]; then
                 BuySell="Sell"
                else
                        BuySell="Buy"
                fi
         fi
         #str="$ccyPair","$counterParty","${option_arr[${i}]}","$BuySell","$bidPrice","$askPrice","$tradeDate","$expiryDate"    
 done

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

loop does not execute in bash script?

I have a very basic bash shell script, which has many "while... done; for .... done" loop clauses, like the following ~~ #!/bin/bash while blablalba; do .... done < /tmp/file for line in `cat blablabla`; do grep $line /tmp/raw ; done > /tmp/1; while blablalba2; do .... done <... (2 Replies)
Discussion started by: fedora
2 Replies

2. Shell Programming and Scripting

error in bash script 'if' loop

SEND_MESSAGE=test echo $SEND_MESSAGE if then echo `date` > update_dt_ccaps.lst echo "The file transfer failed" >> update_dt_ccaps.lst SEND_MESSAGE=false fi The above code is showing error in bash shell as : ./test: line 5: [: test: integer expression expected ... (2 Replies)
Discussion started by: DILEEP410
2 Replies

3. Shell Programming and Scripting

Bash loop script for specfic intervals

Hello, first of all I am happy to sign up here. Next is, I have shell scripts for all the files I want looped infinitely for specific intervals(This is for a wmii config). My question here is how can I run multiple scripts at a 10 second interval for instance? (4 Replies)
Discussion started by: Mesher
4 Replies

4. Shell Programming and Scripting

Whitespace in filenames in for loop in bash script

I'm trying to search all .odt files in a directory for a string in the text of the file. I've found a bash script that works, except that it can't handle whitespace in the filenames. #!/bin/bash if ; then echo "Usage: searchodt searchterm" exit 1 fi for file in $(ls *.odt); do ... (4 Replies)
Discussion started by: triplemaya
4 Replies

5. Shell Programming and Scripting

Expect script called in loop from Bash Script

Having issues with an expect script. I've been scripting bash, python, etc... for a couple years now, but just started to try and use Expect. Trying to create a script that takes in some arguments, and then for now, just runs a pwd command(for testing, final will be command I pass). Here is... (0 Replies)
Discussion started by: cbo0485
0 Replies

6. Shell Programming and Scripting

Bash script - loop question

Hi Folks, I have a loop that goes through an array and the output is funky. sample: array=( 19.239.211.30 ) for i in "${array}" do echo $i iperf -c $i -P 10 -x CSV -f b -t 50 | awk 'END{print '$i',$6}' >> $file done Output: 19.239.211.30 19.2390.2110.3 8746886 seems that when... (2 Replies)
Discussion started by: nitrohuffer2001
2 Replies

7. Shell Programming and Scripting

While loop with input in a bash script

I have the following while loop that I put in a script, demo.sh: while read rna; do aawork=$(echo "${rna}" | sed -n -e 's/\(...\)\1 /gp' | sed -f rna.sed) echo "$aawork" | sed 's/ //g' echo "$aawork" | tr ' ' '\012' | sort | sed '/^$/d' | uniq -c | sed 's/*\(*\) \(.*\)/\2: \... (3 Replies)
Discussion started by: faizlo
3 Replies

8. Shell Programming and Scripting

How to use grep in a loop using a bash script?

Dear all, Please help with the following. I have a file, let's call it data.txt, that has 3 columns and approx 700,000 lines, and looks like this: rs1234 A C rs1236 T G rs2345 G T Please use code tags as required by forum rules! I have a second file, called reference.txt,... (1 Reply)
Discussion started by: aberg
1 Replies

9. UNIX for Beginners Questions & Answers

Help with date in bash script for loop from YYYYMMDDHHMM

Hi everyone I need some help I want to create an script which does some processing it takes the two arguments 201901010000 and 201901020200 - so YYYMMDDHHMM I want to split processing into hours from start until end, I dont get why this works but when I add to a future variable... (1 Reply)
Discussion started by: kl1ngac1k
1 Replies

10. UNIX for Beginners Questions & Answers

Help with a bash loop script

Create a single bash script that does the following: a. Print out the number of occurrences for each motif that is found in the bacterial genome and output to a file called motif_count.txt b. Create a fasta file for each motif (so 3 in total) which contains all of the genes and their... (6 Replies)
Discussion started by: dre
6 Replies
Perl::Tidy(3pm) 					User Contributed Perl Documentation					   Perl::Tidy(3pm)

NAME
Perl::Tidy - Parses and beautifies perl source SYNOPSIS
use Perl::Tidy; Perl::Tidy::perltidy( source => $source, destination => $destination, stderr => $stderr, argv => $argv, perltidyrc => $perltidyrc, logfile => $logfile, errorfile => $errorfile, formatter => $formatter, # callback object (see below) dump_options => $dump_options, dump_options_type => $dump_options_type, prefilter => $prefilter_coderef, postfilter => $postfilter_coderef, ); DESCRIPTION
This module makes the functionality of the perltidy utility available to perl scripts. Any or all of the input parameters may be omitted, in which case the @ARGV array will be used to provide input parameters as described in the perltidy(1) man page. For example, the perltidy script is basically just this: use Perl::Tidy; Perl::Tidy::perltidy(); The module accepts input and output streams by a variety of methods. The following list of parameters may be any of a the following: a filename, an ARRAY reference, a SCALAR reference, or an object with either a getline or print method, as appropriate. source - the source of the script to be formatted destination - the destination of the formatted output stderr - standard error output perltidyrc - the .perltidyrc file logfile - the .LOG file stream, if any errorfile - the .ERR file stream, if any dump_options - ref to a hash to receive parameters (see below), dump_options_type - controls contents of dump_options dump_getopt_flags - ref to a hash to receive Getopt flags dump_options_category - ref to a hash giving category of options dump_abbreviations - ref to a hash giving all abbreviations The following chart illustrates the logic used to decide how to treat a parameter. ref($param) $param is assumed to be: ----------- --------------------- undef a filename SCALAR ref to string ARRAY ref to array (other) object with getline (if source) or print method If the parameter is an object, and the object has a close method, that close method will be called at the end of the stream. source If the source parameter is given, it defines the source of the input stream. destination If the destination parameter is given, it will be used to define the file or memory location to receive output of perltidy. stderr The stderr parameter allows the calling program to capture the output to what would otherwise go to the standard error output device. perltidyrc If the perltidyrc file is given, it will be used instead of any .perltidyrc configuration file that would otherwise be used. argv If the argv parameter is given, it will be used instead of the @ARGV array. The argv parameter may be a string, a reference to a string, or a reference to an array. If it is a string or reference to a string, it will be parsed into an array of items just as if it were a command line string. dump_options If the dump_options parameter is given, it must be the reference to a hash. In this case, the parameters contained in any perltidyrc configuration file will be placed in this hash and perltidy will return immediately. This is equivalent to running perltidy with --dump-options, except that the perameters are returned in a hash rather than dumped to standard output. Also, by default only the parameters in the perltidyrc file are returned, but this can be changed (see the next parameter). This parameter provides a convenient method for external programs to read a perltidyrc file. An example program using this feature, perltidyrc_dump.pl, is included in the distribution. Any combination of the dump_ parameters may be used together. dump_options_type This parameter is a string which can be used to control the parameters placed in the hash reference supplied by dump_options. The possible values are 'perltidyrc' (default) and 'full'. The 'full' parameter causes both the default options plus any options found in a perltidyrc file to be returned. dump_getopt_flags If the dump_getopt_flags parameter is given, it must be the reference to a hash. This hash will receive all of the parameters that perltidy understands and flags that are passed to Getopt::Long. This parameter may be used alone or with the dump_options flag. Perltidy will exit immediately after filling this hash. See the demo program perltidyrc_dump.pl for example usage. dump_options_category If the dump_options_category parameter is given, it must be the reference to a hash. This hash will receive a hash with keys equal to all long parameter names and values equal to the title of the corresponding section of the perltidy manual. See the demo program perltidyrc_dump.pl for example usage. dump_abbreviations If the dump_abbreviations parameter is given, it must be the reference to a hash. This hash will receive all abbreviations used by Perl::Tidy. See the demo program perltidyrc_dump.pl for example usage. prefilter A code reference that will be applied to the source before tidying. It is expected to take the full content as a string in its input, and output the transformed content. postfilter A code reference that will be applied to the tidied result before outputting. It is expected to take the full content as a string in its input, and output the transformed content. Note: A convenient way to check the function of your custom prefilter and postfilter code is to use the --notidy option, first with just the prefilter and then with both the prefilter and postfilter. See also the file filter_example.pl in the perltidy distribution. EXAMPLE
The following example passes perltidy a snippet as a reference to a string and receives the result back in a reference to an array. use Perl::Tidy; # some messy source code to format my $source = <<'EOM'; use strict; my @editors=('Emacs', 'Vi '); my $rand = rand(); print "A poll of 10 random programmers gave these results: "; foreach(0..10) { my $i=int ($rand+rand()); print " $editors[$i] users are from Venus" . ", " . "$editors[1-$i] users are from Mars" . " "; } EOM # We'll pass it as ref to SCALAR and receive it in a ref to ARRAY my @dest; perltidy( source => $source, destination => @dest ); foreach (@dest) {print} Using the formatter Callback Object The formatter parameter is an optional callback object which allows the calling program to receive tokenized lines directly from perltidy for further specialized processing. When this parameter is used, the two formatting options which are built into perltidy (beautification or html) are ignored. The following diagram illustrates the logical flow: |-- (normal route) -> code beautification caller->perltidy->|-- (-html flag ) -> create html |-- (formatter given)-> callback to write_line This can be useful for processing perl scripts in some way. The parameter $formatter in the perltidy call, formatter => $formatter, is an object created by the caller with a "write_line" method which will accept and process tokenized lines, one line per call. Here is a simple example of a "write_line" which merely prints the line number, the line type (as determined by perltidy), and the text of the line: sub write_line { # This is called from perltidy line-by-line my $self = shift; my $line_of_tokens = shift; my $line_type = $line_of_tokens->{_line_type}; my $input_line_number = $line_of_tokens->{_line_number}; my $input_line = $line_of_tokens->{_line_text}; print "$input_line_number:$line_type:$input_line"; } The complete program, perllinetype, is contained in the examples section of the source distribution. As this example shows, the callback method receives a parameter $line_of_tokens, which is a reference to a hash of other useful information. This example uses these hash entries: $line_of_tokens->{_line_number} - the line number (1,2,...) $line_of_tokens->{_line_text} - the text of the line $line_of_tokens->{_line_type} - the type of the line, one of: SYSTEM - system-specific code before hash-bang line CODE - line of perl code (including comments) POD_START - line starting pod, such as '=head' POD - pod documentation text POD_END - last line of pod section, '=cut' HERE - text of here-document HERE_END - last line of here-doc (target word) FORMAT - format section FORMAT_END - last line of format section, '.' DATA_START - __DATA__ line DATA - unidentified text following __DATA__ END_START - __END__ line END - unidentified text following __END__ ERROR - we are in big trouble, probably not a perl script Most applications will be only interested in lines of type CODE. For another example, let's write a program which checks for one of the so-called naughty matching variables "&`", $&, and "$'", which can slow down processing. Here is a write_line, from the example program find_naughty.pl, which does that: sub write_line { # This is called back from perltidy line-by-line # We're looking for $`, $&, and $' my ( $self, $line_of_tokens ) = @_; # pull out some stuff we might need my $line_type = $line_of_tokens->{_line_type}; my $input_line_number = $line_of_tokens->{_line_number}; my $input_line = $line_of_tokens->{_line_text}; my $rtoken_type = $line_of_tokens->{_rtoken_type}; my $rtokens = $line_of_tokens->{_rtokens}; chomp $input_line; # skip comments, pod, etc return if ( $line_type ne 'CODE' ); # loop over tokens looking for $`, $&, and $' for ( my $j = 0 ; $j < @$rtoken_type ; $j++ ) { # we only want to examine token types 'i' (identifier) next unless $$rtoken_type[$j] eq 'i'; # pull out the actual token text my $token = $$rtokens[$j]; # and check it if ( $token =~ /^$[`&']$/ ) { print STDERR "$input_line_number: $token "; } } } This example pulls out these tokenization variables from the $line_of_tokens hash reference: $rtoken_type = $line_of_tokens->{_rtoken_type}; $rtokens = $line_of_tokens->{_rtokens}; The variable $rtoken_type is a reference to an array of token type codes, and $rtokens is a reference to a corresponding array of token text. These are obviously only defined for lines of type CODE. Perltidy classifies tokens into types, and has a brief code for each type. You can get a complete list at any time by running perltidy from the command line with perltidy --dump-token-types In the present example, we are only looking for tokens of type i (identifiers), so the for loop skips past all other types. When an identifier is found, its actual text is checked to see if it is one being sought. If so, the above write_line prints the token and its line number. The formatter feature is relatively new in perltidy, and further documentation needs to be written to complete its description. However, several example programs have been written and can be found in the examples section of the source distribution. Probably the best way to get started is to find one of the examples which most closely matches your application and start modifying it. For help with perltidy's pecular way of breaking lines into tokens, you might run, from the command line, perltidy -D filename where filename is a short script of interest. This will produce filename.DEBUG with interleaved lines of text and their token types. The -D flag has been in perltidy from the beginning for this purpose. If you want to see the code which creates this file, it is "write_debug_entry" in Tidy.pm. EXPORT
&perltidy CREDITS
Thanks to Hugh Myers who developed the initial modular interface to perltidy. VERSION
This man page documents Perl::Tidy version 20101217. AUTHOR
Steve Hancock perltidy at users.sourceforge.net SEE ALSO
The perltidy(1) man page describes all of the features of perltidy. It can be found at http://perltidy.sourceforge.net. perl v5.10.1 2010-12-13 Perl::Tidy(3pm)
All times are GMT -4. The time now is 10:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy