Results 1 to 13 of 13

Thread: Help please

  1. #1
    Join Date
    Sep 2006
    Posts
    39

    Red face Help please

    Hello everybody

    Please i need a help in creating an sh file to be run if an condition happend

    I need to search in a file for a specific word and if its found then to run some commands

    the commands i need as below

    egrep "word" /home/*/public_html -R | cut -d: -f1 | uniq
    if word found then
    cd /home
    mv folder folder1
    tar xvzf file.tar.gz
    rm -rf folder1
    end if

    i need a working sh file with correct syntax do that

    hope to find who can help me please

    thanks

  2. #2
    Join Date
    Nov 2003
    Location
    Kherson, Ukraine
    Posts
    267
    if you do not need to use found filenames in the second part of script you can do like

    Code:
    #/bin/sh
    
    egrep "word" /home/*/public_html -R | cut -d: -f1 | uniq
    
    if [ $? != 0 ]; then
      cd /home
      mv folder folder1
      tar xvzf file.tar.gz
      rm -rf folder1
    fi
    Last edited by vapetrov; 08-04-2009 at 05:57 PM.
    Private remote administrator of Linux servers - www.petrov.ks.ua
    Quality hosting - Host-Web-Site.com

  3. #3
    Join Date
    Nov 2003
    Location
    Kherson, Ukraine
    Posts
    267
    If you need to pass found values to second part of script:
    Code:
    #/bin/sh
    
    $RESULTS=`egrep "word" /home/*/public_html -R | cut -d: -f1 | uniq`
    
    for $RES in $RESULTS; do
      cd /home
      mv $RES folder1
      tar xvzf file.tar.gz
      rm -rf folder1
    fi
    Private remote administrator of Linux servers - www.petrov.ks.ua
    Quality hosting - Host-Web-Site.com

  4. #4
    Join Date
    Sep 2006
    Posts
    39
    Great !

    Thank you very much vapetrov

  5. #5
    Join Date
    Sep 2006
    Posts
    39
    Hi agian

    i need also to count the results found

    if resutls > 5 then
    do somthing
    end if

    how can i make it excutable ( sh file )

    Many Thanks

  6. #6
    Join Date
    Sep 2007
    Posts
    368

    *

    Quote Originally Posted by majdiy View Post
    Hi agian

    i need also to count the results found

    if resutls > 5 then
    do somthing
    end if

    how can i make it excutable ( sh file )

    Many Thanks
    Hi

    about if condition.

    Code:
    if [ $resultx -gt 5 ]; then
    
    echo "value greater"
    
    else
    
    echo "value small"
    
    fi
    executable.

    you can do it,

    # chmod +x your-script-name [make your script executable]

    # ./your-script-name [run your script]

  7. #7
    Join Date
    Sep 2006
    Posts
    39
    Hi nomankhn

    Thank you very much for your help

    The code below is exactly what i want to do

    #/bin/sh
    $RESULTS=`egrep "an word to find" /home/*/public_html -R | cut -d: -f1 | uniq`if [ $RESULTS -gt 5 ] then echo "value greater" else echo "value small" fi

    I added it in a file name test.sh

    when i run it

    [email protected] [~]# sh test.sh
    test.sh: line 2: =/home/sitecom/public_html/file1.htm: No such file or directory

    can you please make the sh file correctly to do the task required

    Thank you

  8. #8
    Join Date
    Sep 2007
    Posts
    368

    *

    Quote Originally Posted by majdiy View Post
    Hi nomankhn

    Thank you very much for your help

    The code below is exactly what i want to do

    #/bin/sh
    $RESULTS=`egrep "an word to find" /home/*/public_html -R | cut -d: -f1 | uniq`if [ $RESULTS -gt 5 ] then echo "value greater" else echo "value small" fi

    I added it in a file name test.sh

    when i run it

    [email protected] [~]# sh test.sh
    test.sh: line 2: =/home/sitecom/public_html/file1.htm: No such file or directory

    can you please make the sh file correctly to do the task required

    Thank you

    Hi,

    your code having some issue, i am checking.

  9. #9
    Join Date
    Sep 2007
    Posts
    368

  10. #10
    Join Date
    Sep 2006
    Posts
    39
    Code:
     
    #/bin/sh
    findresult=`grep -R -o "<script>" /home/*/public_html/ | grep -v ":0" | wc -l`
    if [  $findresult -gt 5 ]; then 
    echo $findresult
    echo "value greater" 
    else 
    echo $findresult
    echo "value small" 
    fi

    [email protected] [~]# sh test.sh
    : command not found
    : command not found
    : integer expression expected
    : command not found
    8
    value small
    : command not found

  11. #11
    Join Date
    Sep 2007
    Posts
    368

    *

    Quote Originally Posted by majdiy View Post

    [email protected] [~]# sh test.sh
    : command not found
    : command not found
    : integer expression expected
    : command not found
    8
    value small
    : command not found
    looks like grep command or something is missing because same command is working at my end. change this line grep with egrep then check.

    findresult=`egrep -R -o "<script>" /home/*/public_html/ | grep -v ":0" | wc -l`


    or if you make the script file executable then run it like # ./script name

    make script executable -> chmod +x scriptname
    Last edited by nomankhn; 08-09-2009 at 12:06 PM.

  12. #12
    Join Date
    Sep 2006
    Posts
    39
    I did
    The same results

    The command i use in the command line is
    egrep "<script>" /home/*/public_html -R | cut -d: -f1 | uniq

    The results
    [email protected] [~]# egrep "<script>" /home/*/public_html -R | cut -d: -f1 | uniq
    /home/cools4u/public_html/klemat.htm
    /home/linksco/public_html/payment/authorizenet.check-invoice.php
    /home/linksco/public_html/payment/authorizenet.check-upgrade.php
    /home/linksco/public_html/cache/rss_854c1e536056b60d4c984d18bb8e8e03.cache
    /home/vbcd1f2/public_html/includes/adminfunctions.php
    /home/videoco/public_html/com.php

    6 results

    What i need at the end if the results more than 6 to do something like sending email or running a php file to send me email

  13. #13
    Join Date
    Sep 2007
    Posts
    368

    *

    Quote Originally Posted by majdiy View Post
    I did
    The same results

    The command i use in the command line is
    egrep "<script>" /home/*/public_html -R | cut -d: -f1 | uniq

    The results
    [email protected] [~]# egrep "<script>" /home/*/public_html -R | cut -d: -f1 | uniq
    /home/cools4u/public_html/klemat.htm
    /home/linksco/public_html/payment/authorizenet.check-invoice.php
    /home/linksco/public_html/payment/authorizenet.check-upgrade.php
    /home/linksco/public_html/cache/rss_854c1e536056b60d4c984d18bb8e8e03.cache
    /home/vbcd1f2/public_html/includes/adminfunctions.php
    /home/videoco/public_html/com.php

    6 results

    What i need at the end if the results more than 6 to do something like sending email or running a php file to send me email
    You can check this.

    emailfile=`mktemp /tmp/emailfile.XXXX`


    egrep "<script>" /home/*/public_html -R | cut -d: -f1 | uniq > $emailfile

    if [ $results -gt 6 ] ; then
    echo ..........

    mail -s "confidential" [email protected] < $emailfile

Posting Permissions

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