Find the answer to your Linux question:
Results 1 to 4 of 4
Hello, I have recently moved to Kubuntu and I need to convert some win command.com script to linux script. The WIN script is: ## start code ## @echo off for ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Posts
    1

    Help with Automated file search and replacement.

    Hello,

    I have recently moved to Kubuntu and I need to convert some win command.com script to linux script.

    The WIN script is:
    ## start code ##
    @echo off
    for /R %%x in (.) do if exist %%x\title. (
    set path=%%x
    set var=!path:~7,-10!
    echo !var! > !path!\title
    )
    ##end code##
    The code starts in some directory and recursively goes through the directory putting the path of each subdirectory into the '%%x' variable the if statement tests to see if the subdirectory has a file called title in it, if yes the contents of the variable '%%x' are set to the string 'path'. A subset of the contents of the 'path' variable are put into the 'var' variable. The contents of the 'var' variable are then echoed into the 'title' file of the subdirectory in question. Note. before you ask the in windows the '%%x' variable can't be manipulate as a string hence the dump into the 'path' variable.

    Basically I am very new to linux script and would like some pointers to help me convert this code quickly as I need it very quickly.


    many thanks

    Mark

  2. #2
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Well take a look at a function I once wrote as a novelty exercise that emulates the find command with nothing but shell built-ins:

    Code:
    #!/bin/bash
    function myfind {
       for FILE in *; do
          if [ $FILE = '*' ]; then
             continue
          elif [ -d $FILE ]; then
             echo ${PWD}\/$FILE
             pushd $FILE > /dev/null 2>&1
             if [ $? -ne 0 ]; then            
                echo "Permission to enter $FILE denied."
                continue
             fi
             myfind
          else
             echo ${PWD}\/$FILE
             continue
          fi
          popd >/dev/null 2>&1
       done
    }
    
    myfind
    So, the algorithm for doing this is in there, except that prints out all files in each directory. When you're in each directory instead of echoing the filenames, you will do

    Code:
    if [ -e title ]; then
       dirs +0 > title
    fi
    If you have questions about what this code does, just reply to the thread.

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    you can use find .
    Code:
    find /yourpath -type f -name "title" -print
    you can pipe out the results for further processing.

  4. #4
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Yea, that would actually probably be easier to someone going from windows scripting to bash, I instantly just thought of that function I posted just cause I don't think anything else ever presented itself that had a practical use for it.

Posting Permissions

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