Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Nesting while loops


    I created a script that renames folders and files (the file part has not been implemented). The primary goal involves placing all regex patterns in preset files separated by whitespace. This functionality allows me to have presets for renaming folders, mp3, html, php, or css files. In addition, I need to use the same regular expression patterns for the file rename code block; and the folder rename code block.

    Can Bash nest while loops with the read command?

    Does a better approach exist for passing an external file with regex patterns and then iterating through files and folders?


    I wrote the script in Bash; just because I am more comfortable with this scripting environment. Maybe in the near future, I’ll port this script to a Python or Perl solution. So I am really hoping to figure this task in Bash before moving to another development sphere. But comments about other ways to accomplish it in Perl/Python would be helpful.

    Here’s the error message from the console.
    FileRenamer.sh: line 41: syntax error near unexpected token done
    FileRenamer.sh: line 41: ` done < "$PRESET"

    Here’s the pseudo code (reduced for simplicity).

    Code:
    #!/usr/bin/bash -x 
    
    REQPARAM=1
    PRESET="~/bin/preset.txt"
    
    # Position Parameter for passing the directory.
    if [ $# = $REQPARAM ]
    then 
    	DIRWFILES="$1"
    	# change to directory specified in
    	# positional parameter -just in case
    	cd "$DIRWFILES"
    elif [ $# != $REQPARAM ]
    then
    	echo 'You have left out the parameter for the folder with the files'
    	echo 'Current directory will be used instead'
    	DIRWFILES=`pwd`
    fi
    
    find "$DIRWFILES" -depth -type d -print | while read DIR
      do
        while read REGEX REPLACE line
        do
         NEWNAME=$(echo "$DIR" | sed 's/"$REGEX"/"$REPLACE"/g')
            echo "mv "$DIR" "$NEWNAME""
            DIR=”$NEWNAME”
            sleep 2
        done < "$PRESET"
      done
      
    # END

  2. #2
    currently you have

    while ... < "data"
    can u change it as
    echo "data" | while ...
    this must work

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •