Sponsored Content
Top Forums Shell Programming and Scripting Help with looping in shell scripting Post 302588402 by drl on Monday 9th of January 2012 12:54:48 AM
Old 01-09-2012
Hi.

Here is a comparison of the shell script and perl script. Both produce a line at "2345" so that you can see they are working and that the calculations match:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate shell vs perl for specific md5 value.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C md5sum perl

time {
first=89752
last=89760
pl " Results, $first ... $last, all in shell:"
for val in $( seq -w $first $last )
do
  hash=$( echo $val | md5sum )
  db " hash is $hash"
  [ $val -eq 2345 ] && pe " $val -> $hash, shell md5sum"
  [[ $hash =~ ^0000 ]] &&
  echo "$val produced 0000 hash string: $hash" ||
  echo $val >> log
done
}

time {
first=0000
last=9999
pl " Results, $first ... $last, all in shell:"
for val in $( seq -w $first $last )
do
  hash=$( echo $val | md5sum )
  db " hash is $hash"
  [ $val -eq 2345 ] && pe " $val -> $hash, shell md5sum"
  [[ $hash =~ ^0000 ]] &&
  echo "$val produced 0000 hash string" ||
  echo $val >> log
done
}

time {
first=2345
last=2346
pl " Results, $first ... $last, in perl:"
./p1 $first $last
}
time {
first=0000
last=9999
pl " Results, $first ... $last, in perl:"
./p1 $first $last
}

time {
first=00000
last=99999
pl " Results, $first ... $last, in perl:"
./p1 $first $last
}

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
GNU bash 3.2.39
md5sum (GNU coreutils) 6.10
perl 5.10.0

-----
 Results, 89752 ... 89760, all in shell:
89752 produced 0000 hash string: 00009d1a40b9e983bef130790f3101e5  -

real	0m0.051s
user	0m0.008s
sys	0m0.020s

-----
 Results, 0000 ... 9999, all in shell:
 2345 -> c47abe049e90cd2d285fd697ca4a8c6a  -, shell md5sum

real	0m32.657s
user	0m7.616s
sys	0m22.637s

-----
 Results, 2345 ... 2346, in perl:
 2345 -> c47abe049e90cd2d285fd697ca4a8c6a, perl Digest::MD5 (md5_hex)

real	0m0.015s
user	0m0.012s
sys	0m0.000s

-----
 Results, 0000 ... 9999, in perl:
 2345 -> c47abe049e90cd2d285fd697ca4a8c6a, perl Digest::MD5 (md5_hex)

real	0m0.058s
user	0m0.040s
sys	0m0.004s

-----
 Results, 00000 ... 99999, in perl:
 02345 -> 9d129bafb8d74f63aca3d3015269b48c, perl Digest::MD5 (md5_hex)
89752 produces md5 hash of 00009d1a40b9e983bef130790f3101e5

real	0m0.274s
user	0m0.244s
sys	0m0.008s

I am not suggesting that you must do this in perl, but I think you can see that shell is probably not the best choice.

Best wishes ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

2. Shell Programming and Scripting

looping through a variable in a shell script

hi, my first question is :- i would like to know how do i loop through the output of a variable. for ex:- if i have a variable called x and echo $x gives the output like feb 19 07 feb 20 07 feb 21 07 i would like to know how do i loop through this since it is separated and i... (1 Reply)
Discussion started by: ramachandranrr
1 Replies

3. Shell Programming and Scripting

Convert shell script for looping

Situation: I have a working shell script on our file server (OSXS Tiger) to connect to a workstation, which is using a portable home directory (phd), and rsync a user's MirrorAgent.log. I'm not that strong of a scripter (obviously), but I would like to add other workstations to this script as they... (4 Replies)
Discussion started by: le0pard13
4 Replies

4. Shell Programming and Scripting

Looping through a shell script with sql statements

Hello members, I'm working on the Solaris environment and the DB i'm using is Oracle 10g. Skeleton of what I'm attempting; Write a ksh script to perform the following. I have no idea how to include my sql query within a shell script and loop through the statements. Have therefore given a... (4 Replies)
Discussion started by: novice82
4 Replies

5. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

6. Shell Programming and Scripting

[Solved] help me in this looping in shell scripting

Hi, #!/bin/ksh result='/TIA/app/UniQP/queue/document/CSB' i=0; while ; do i=`expr $i + 1` if ($i -lt 5);then echo "THerrFile_$i.err"; else break; fi done ... (0 Replies)
Discussion started by: sudhir_83k
0 Replies

7. Shell Programming and Scripting

C Shell Script: While function not fully looping

I am new to scripting and this is probably the 4th or 5th simple script I have written. I am working with a HUGE number of data that need to be organized into folders and named a certain way. I wrote the naming script using a while function to go through the 1000-some folders and rename the files... (0 Replies)
Discussion started by: notluckyhannah
0 Replies

8. Shell Programming and Scripting

Looping not completing in shell script

Hi, Iam using below code to login to servers to get cpu utilisation. but output is coming for only one server. code is below root@blr-svr-oclan-01 # more SSSC_CPU_UTIL1.sh #!/bin/sh echo "CPU UTILIZATION" while read line; do IDLE=`/usr/local/bin/sshpass -p 'xxx' ssh xxx@$line 'sar 2 2' |... (1 Reply)
Discussion started by: surender reddy
1 Replies

9. Shell Programming and Scripting

Simple looping in shell

Hii all. I have a problem with my shell script. This is my code while do space.exe inputs/gr$count >> outputs/t$count count=$ done I ussually work in cygwin. When i tried in linux(ubuntu, kali linux) i found error said Syntax error : end of file unexpected (expecting "do")... (6 Replies)
Discussion started by: weslyarfan
6 Replies

10. Shell Programming and Scripting

Shell scripting looping issue

#!bin/ksh --------------------------------------------------------------------------------------------- -- Get sequence number from database --------------------------------------------------------------------------------------------- .os rm... (3 Replies)
Discussion started by: swathi reddy1
3 Replies
hash(1) 							   User Commands							   hash(1)

NAME
hash, rehash, unhash, hashstat - evaluate the internal hash table of the contents of directories SYNOPSIS
/usr/bin/hash [utility] /usr/bin/hash [-r] sh hash [-r] [name]... csh rehash unhash hashstat ksh hash [name]... hash [-r] DESCRIPTION
/usr/bin/hash The /usr/bin/hash utility affects the way the current shell environment remembers the locations of utilities found. Depending on the argu- ments specified, it adds utility locations to its list of remembered locations or it purges the contents of the list. When no arguments are specified, it reports on the contents of the list. The -r option causes the shell to forget all remembered locations. Utilities provided as built-ins to the shell are not reported by hash. sh For each name, the location in the search path of the command specified by name is determined and remembered by the shell. The -r option to the hash built-in causes the shell to forget all remembered locations. If no arguments are given, hash provides information about remem- bered commands. The Hits column of output is the number of times a command has been invoked by the shell process. The Cost column of output is a measure of the work required to locate a command in the search path. If a command is found in a "relative" directory in the search path, after changing to that directory, the stored location of that command is recalculated. Commands for which this will be done are indi- cated by an asterisk (*) adjacent to the Hits information. Cost will be incremented when the recalculation is done. csh rehash recomputes the internal hash table of the contents of directories listed in the path environmental variable to account for new com- mands added. unhash disables the internal hash table. hashstat prints a statistics line indicating how effective the internal hash table has been at locating commands (and avoiding execs). An exec is attempted for each component of the path where the hash function indicates a possible hit and in each component that does not begin with a '/'. ksh For each name, the location in the search path of the command specified by name is determined and remembered by the shell. The -r option to the hash built-in causes the shell to forget all remembered locations. If no arguments are given, hash provides information about remem- bered commands. OPERANDS
The following operand is supported by hash: utility The name of a utility to be searched for and added to the list of remembered locations. OUTPUT
The standard output of hash is used when no arguments are specified. Its format is unspecified, but includes the pathname of each utility in the list of remembered locations for the current shell environment. This list consists of those utilities named in previous hash invoca- tions that have been invoked, and may contain those invoked and found through the normal command search process. ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of hash: LANG, LC_ALL, LC_CTYPE, LC_MES- SAGES, and NLSPATH. PATH Determine the location of utility. EXIT STATUS
The following exit values are returned by hash: 0 Successful completion. >0 An error occurred. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ SEE ALSO
csh(1), ksh(1), sh(1), attributes(5), environ(5), standards(5) SunOS 5.11 17 Jul 2002 hash(1)
All times are GMT -4. The time now is 11:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy