Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Repetitive scripts within a bash shell Post 302552591 by yazu on Sunday 4th of September 2011 11:10:17 PM
Old 09-05-2011
Standard refactoring steps:
1. Format you code - http://www.linux-kheops.com/doc/perl...ert/fmt.script and gawk --profile can help. (here you need only in gawk pretty-printing facilities)
2. Rename variables.
3. Extract functions,
4. Comment.
5. Do everything in small steps, test continuously and save you changes in git (it's really easy and very, very useful).

In addition to bartus11:
Code:
# comment and rename
function f {         
  i=$1  # comment
  e=$2  # comment and rename
  
  muscle -in ${e}.$i > $e.$(( i+1 ))
  
  read -t1 # do you really need it (at least here)?
  # comment 
  sed ':a /^>/!N;s/\n\([^>]\)/\1/;ta' $e.$(( i+1 )) > $e.$(( i+2 ))
  
  read -t2 # comment
  # comment
  awk '
    />/  { # comment, rename fr
      fr = $3              
      getline
      n = split($0, a, "")
      for (i = 1; i <= n; i++) {
        b[(i "-" a[i])] += fr
      }
    }
    # comment
    END {
      for (i in b) {
        split(i, c, "-")
        if (d[c[1]] <= b[i]) { # complex algorithm, comment
          e[c[1]] = c[2]
          d[c[1]] = b[i]
        }
      }
      for (i in e) {
        print (i " " e[i])
      }
    }
  ' - $e.$(( i+2 )) > $e.$(( i+3 ))          # what is in stdin?

  read -t2 # ??? no need
  # comment
  awk -vRS=">" '
    length($0) > 0  { # comment
      a[$4] += $3
      b[$4] = $1
    }
    END {                             # comment
      for (i in a) {
        printf (">" b[i] " Freq " a[i] "\n" i "\n")
      }
    }
  ' $e.$(( i+3 )) > $e.$(( i+4 ))
}

You can split this function father (to aliases, functions, command variables) to define easy understandable and testable small steps.

===

Well, after refactoring you should change this function to something like this (names should be renamed, of course):

Code:
step1_muscle_command INPUTFILE | 
step2_sed_processing  | 
step3_awk1_processing | 
step4_awk2_processing >OUTPUTFILE

and wrap this in a loop.

Last edited by yazu; 09-05-2011 at 01:25 AM..
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Bash Shell Scripts

Hi all, plz can anybody tell me that if a script written for Bash shell also work for other shells and if yes , how ??? Thanks and Regards SONAL (2 Replies)
Discussion started by: sonbag_pspl
2 Replies

2. Shell Programming and Scripting

Changing the Bash Scripts to Bourne Scripts:URGENT

Hi, I have to write a program to compute the checksums of files ./script.sh I wrote the program using bash and it took me forever since I am a beginner but it works very well. I'm getting so close to the deadline and I realised today that actually I have to use normal Bourne shell... (3 Replies)
Discussion started by: pgarg1989
3 Replies

3. Shell Programming and Scripting

best way for removing comment from shell scripts -- bash

Again a comment removal requirement from me, refer my previous problem & solution for removing comment from ruby scripts: https://www.unix.com/shell-programming-scripting/118296-best-way-removing-comment-ruby-program.html This time, it is for stripping of comments from Shell Script. I search for... (2 Replies)
Discussion started by: thegeek
2 Replies

4. Shell Programming and Scripting

Create Bash shell scripts corresponding to windows bat files

Experts, I am newbie in shell scripting. I want to write Bash shell scripts corresponding to windows bat files. I have installed cygwin at c:\cygwin and i am trying to crate the sh file using vi editor. i am not able to understand how to use linux/unix convention for the code. following is my... (1 Reply)
Discussion started by: rajuchacha007
1 Replies

5. Shell Programming and Scripting

Writing Bash shell scripts corresponding to windows bat files

Experts, I am newbie in shell scripting. I want to write Bash shell scripts corresponding to windows bat files. I have installed cygwin at c:\cygwin and i am trying to crate the sh file using vi editor. i am not able to understand how to use linux/unix convention for the code. following is my... (15 Replies)
Discussion started by: rajuchacha007
15 Replies

6. Shell Programming and Scripting

Paid job for bash shell scripts

Job ad removed (1 Reply)
Discussion started by: starmation
1 Replies

7. Homework & Coursework Questions

Bash shell scripts

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Test that exactly one command line argrument is enter from the command line. If not, display the usage... (1 Reply)
Discussion started by: jcoop12
1 Replies

8. Shell Programming and Scripting

Difference between kshell and bash shell scripts Example cited

Hi All, I need some urgent help regarding some info. I have a cluster of servers for which I have two scripts for management. control.sh is a bash script meant for restarting/stopping the servers. manger.ksh is a kshell script. It is a master script to manage restarting/stoppping and... (3 Replies)
Discussion started by: ankur328
3 Replies

9. Shell Programming and Scripting

Usage of #!/bin/sh vs #!/bin/bash shell scripts?

Some question about the usage of shell scripts: 1.) Are the commands of the base shell scripts a subset of bash commands? 2.) Assume I got a long, long script WITHOUT the first line. How can I find out if the script was originally designed für "sh" or "bash"? 3.) How can I check a given... (3 Replies)
Discussion started by: pstein
3 Replies
WRJPGCOM(1)						      General Commands Manual						       WRJPGCOM(1)

NAME
wrjpgcom - insert text comments into a JPEG file SYNOPSIS
wrjpgcom [ -replace ] [ -comment text ] [ -cfile name ] [ filename ] DESCRIPTION
wrjpgcom reads the named JPEG/JFIF file, or the standard input if no file is named, and generates a new JPEG/JFIF file on standard output. A comment block is added to the file. The JPEG standard allows "comment" (COM) blocks to occur within a JPEG file. Although the standard doesn't actually define what COM blocks are for, they are widely used to hold user-supplied text strings. This lets you add annotations, titles, index terms, etc to your JPEG files, and later retrieve them as text. COM blocks do not interfere with the image stored in the JPEG file. The maximum size of a COM block is 64K, but you can have as many of them as you like in one JPEG file. wrjpgcom adds a COM block, containing text you provide, to a JPEG file. Ordinarily, the COM block is added after any existing COM blocks; but you can delete the old COM blocks if you wish. OPTIONS
Switch names may be abbreviated, and are not case sensitive. -replace Delete any existing COM blocks from the file. -comment text Supply text for new COM block on command line. -cfile name Read text for new COM block from named file. If you have only one line of comment text to add, you can provide it on the command line with -comment. The comment text must be sur- rounded with quotes so that it is treated as a single argument. Longer comments can be read from a text file. If you give neither -comment nor -cfile, then wrjpgcom will read the comment text from standard input. (In this case an input image file name MUST be supplied, so that the source JPEG file comes from somewhere else.) You can enter multiple lines, up to 64KB worth. Type an end-of-file indicator (usually control-D) to terminate the comment text entry. wrjpgcom will not add a COM block if the provided comment string is empty. Therefore -replace -comment "" can be used to delete all COM blocks from a file. EXAMPLES
Add a short comment to in.jpg, producing out.jpg: wrjpgcom -c "View of my back yard" in.jpg > out.jpg Attach a long comment previously stored in comment.txt: wrjpgcom in.jpg < comment.txt > out.jpg or equivalently wrjpgcom -cfile comment.txt < in.jpg > out.jpg SEE ALSO
cjpeg(1), djpeg(1), jpegtran(1), rdjpgcom(1) AUTHOR
Independent JPEG Group 15 June 1995 WRJPGCOM(1)
All times are GMT -4. The time now is 04:39 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy