Find the answer to your Linux question:
Results 1 to 8 of 8
I have searched Google but unable to find good example. Iím new to bash scripting but have learned a lot so far. Anyways here goes the problem I need to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2006
    Posts
    5

    Please help bash script loop


    I have searched Google but unable to find good example.
    Iím new to bash scripting but have learned a lot so far.
    Anyways here goes the problem I need to create a loop.
    That checks a given directory and if exists +1 the number.
    Until it get a number not in use.

    Example:
    Get directory list.
    Files start with a number 80jinzora 81webmail 82etc..
    Iíd like to have it start at 80 and go to maybe 100.
    I only want it to check the first two or three numbers not the rest of the file name.
    As that I would like to have appended by the script I.E 83test.
    If file doesnít exist create file then once file is created continue with rest of script.

    Any Help much appreciated.
    Thanks.

  2. #2
    Linux Enthusiast KenJackson's Avatar
    Join Date
    Jun 2006
    Location
    Maryland, USA
    Posts
    510
    Not sure exactly what you want, but this might help.
    Code:
    for n in $(seq 80 100); do
        if [ -z "$(ls $n* 2>/dev/null)" ]; then
            echo creating ${n}test
            touch ${n}test
        fi
    done
    The command "seq 80 100" prints a list of number from 80 to 100.
    The command "for n in $(seq 80 100)" loops through the numbers assigning $n to each.
    The command "ls $n* 2>/dev/null" lists all files in the current directory that start with $n, but discards errors.

  3. #3
    Just Joined!
    Join Date
    Aug 2006
    Posts
    5
    Here is a list of the directory I would like to use.
    What I'd like to do is have a way not to duplicate numbers of file names.
    For example if number 84 and name test is not taken then create that file.
    Also loop until it finds the next available number then use that number.
    I know that i'm way in over my head on this one.
    P.S Thank you so much for the quick response.

    01localAccessString
    02externalSSLAccessString
    05NoHostLookups
    05ServerAdmin
    05ServerRoot
    05ServerTokens
    05UserGroup
    10ErrorLog
    15DSOstart
    20LoadModule10
    20LoadModule20
    20LoadModule30
    20LoadModule40
    20LoadModule50
    20LoadModule60
    20LoadModule70
    20LoadModule80
    20LoadModule80mod_perl
    20LoadModule80mod_ssl
    20LoadModule80PHP
    35ExternalPwAuth
    35Listen80
    35ProxyPass
    35SSL00Begin
    35SSL00Listen443
    35SSL05SSLEngine
    35SSL10SetEnvIf
    35SSL10SSLCertificateFile
    35SSL10SSLCipherSuite
    35SSL10SSLPassPhraseDialog
    35SSL10SSLSessionCache
    35SSL10SSLSessionCacheTimeout
    35SSL15SSLMutex
    35SSL20SSLRandomSeed
    35SSL25SSLLogLevel
    35SSL30SSLProtocol
    35SSL99End
    40LockFile
    40PidFile
    40ScoreBoardFile
    45CacheNegotiatedDocs
    45CanonicalName
    45CustomLogs
    45FileETag
    45KeepAlive
    45Limits
    45Proxy
    45ServerName
    45ServerPool
    45Timeout
    50DefaultIcon
    50DirectoryIndex00
    50DirectoryIndex00PHP
    50DocRoot
    50FancyIndexingOn
    55AddIconByEncoding00
    60AccessFileName
    60AddDescription00
    60AddIconByType00
    60DefaultType
    60IndexIgnore00
    60TypesConfig
    65AddEncoding00
    65AddIcon00
    65AddIcon00PHP
    65AddLanguage00
    70LanguagePriority00
    75AddType00
    75AddType05.exe
    75AddType05.pac
    75AddType05.png
    75AddType05.rpm
    75AddType05.shtml
    75AddType05.svg
    75AddType05.xsl
    75AddTypes05.crl
    75AddTypes05.crt
    75BrowserMatch00
    80AddHandler00
    80AddHandler05
    80AddHandler10
    80DenyHTAccess
    80NameVirtualHosts
    80VirtualHosts
    85DefaultAccess
    85HordeAccess
    85ImpAccess
    85IngoAccess
    85ServerResourcesAccess
    85TurbaAccess
    86PhpmyadminAlias
    90e-smithAccess00
    90e-smithAccess10icons
    90e-smithAccess40ibays
    92jinzora
    95AddType00PHP2ibays
    95ProxyService
    96ProxyConf

  4. #4
    Just Joined!
    Join Date
    Aug 2006
    Posts
    5
    Ok I think I know the question to ask.
    if condition 1 = 84
    then
    check condition 2 = rest of file name I.E test
    else
    loop till i get the next available number
    then touch 84test or what ever next number.
    I know there are some duplicate numbers but that is part of the server software.
    I don't want to conflict with.
    Thank you for you're help.

  5. #5
    Linux Enthusiast KenJackson's Avatar
    Join Date
    Jun 2006
    Location
    Maryland, USA
    Posts
    510
    Still not sure, but let's modify it.
    Code:
    # Assume you input numeric variable condition1 in some way prior to this point
    
    for n in $(seq $condition1 100); do
        if [ -z "$(ls $n* 2>/dev/null)" ]; then
            echo creating ${n}test
            touch ${n}test
            break;
        fi
    done

  6. #6
    Just Joined!
    Join Date
    Aug 2006
    Posts
    5
    Thank you I understand it now.
    I just added the break part to you're first suggestion and it works like a charm.
    Now I only get one file with the next available number.
    But is there a way to also check the next part of the file name.
    For example the test part as I don't want multiple ??test files.
    only one is required maybe some thing like ??test1 ??test2 ??test3.
    So a recap I would like only one file wich is unique.

    Thanks again Ken.

  7. #7
    Linux Enthusiast KenJackson's Avatar
    Join Date
    Jun 2006
    Location
    Maryland, USA
    Posts
    510
    OK, maybe something like this.
    Code:
    # Assume you input numeric variable condition1 in some way prior to this point
    
    for n in $(seq $condition1 100); do
        if [ -z "$(ls $n* 2>/dev/null)" ]; then
            NAME="${n}test"
    
            # If the base name is already taken, keep incrementing until a unique one is found
            if [ -f $NAME ]; then
                i=1
                while [ -f $NAME$i ]; do
                    i=$((i+1))
                done
                NAME=$NAME$i
            fi
    
            # Create the unique name and exit
            echo creating $NAME
            touch $NAME
            break;
        fi
    done

  8. #8
    Just Joined!
    Join Date
    Aug 2006
    Posts
    5

    Smile

    Yes that is exactly what I wanted.
    Thank you so much for all you're help and willingness to teach.
    I am not new to linux but am new to advanced scripting.
    For some this maybe trivial.
    I realy like the way linux has a community that helps one another.

    Thanks again for the help.
    P.S if I can ever help you just let me know.

Posting Permissions

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