[Script] Conditions on parsing file | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

[Script] Conditions on parsing file

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-20-2013
amazigh42 amazigh42 is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 29 October 2014, 10:58 AM EDT
Posts: 77
Thanks: 11
Thanked 0 Times in 0 Posts
[Script] Conditions on parsing file

Hello,

I open a new POST, i consider that this is resolved
http://www.unix.com/shell-programmin...nt-script.html
But i wish improve it.
In case 1, I would like to test the input file $1.
If $1 exist with no parameters but only comments, then send a message "Please..."
I tried somethings but no results.
Can you help me ?


Code:
#!/bin/bash
# set -vx

f1()    {
         echo $1 $2 $3 $4
        }


case $# in
        0)      echo -e "# comment1\n# comment2" > list_file
                read -p "Please fill in this list_file: "
                echo $REPLY
                ;;
        1)      [ -f "$1" ] || exit
                grep -v '^#' "$1" |
                        while read arg1 arg2 arg3 arg4
                                do f1 $arg1 $arg2 $arg3 $arg4
                                done
                ;;
        4)      echo -e "# comment1\n# comment2" > list_file
                echo $1 $2 $3 $4 >> list_file
                ;;
        5)      echo -e "# comment1\n# comment2" > $1
                f1 $2 $3 $4 $5 >> "$1"
                ;;

        *)      echo "error msg"; exit
esac

Have a nice day.
Sponsored Links
    #2  
Old Unix and Linux 02-20-2013
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 17 February 2015, 1:56 PM EST
Location: Southern NJ, USA (Nord)
Posts: 4,671
Thanks: 8
Thanked 585 Times in 558 Posts

Code:
if [ -f "$1" ]
then
 ...
else
 ...
fi

Sponsored Links
    #3  
Old Unix and Linux 02-21-2013
amazigh42 amazigh42 is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 29 October 2014, 10:58 AM EDT
Posts: 77
Thanks: 11
Thanked 0 Times in 0 Posts
Thanks Unix or Linux Image
It's OK


Code:
#!/bin/bash
# set -vx

f1()    {
         echo $1 $2 $3 $4                                       
        } 


case $# in
        0)      echo -e "# comment1\n# comment2" > list_file    
                read -p "Please fill in this list_file: "      
                echo $REPLY                                     
                ;;
        1)     if [ ! -f "$1" ] 
               then  > $1 && echo -e "# comment1\n# comment2" > $1
               read -p "Please fill in "$1": "
               echo $REPLY 
               else 
                   if [ -f "$1" -a $(grep -v '^#' "$1" | wc -l ) -ne 0 ]                             
                   then
                   grep -v '^#' "$1" |
                        while read arg1 arg2 arg3 arg4
                                do f1 $arg1 $arg2 $arg3 $arg4
                                done  
                   else 
                   read -p "Please fill in $1 : "
                   echo $REPLY
                   fi 
               fi
               ;;  
        4)      echo -e "# comment1\n# comment2" > list_file    
                echo $1 $2 $3 $4 >> list_file                   
                ;;
        5)      echo -e "# comment1\n# comment2" > $1           
                f1 $2 $3 $4 $5 >> "$1"                   
                ;;

        *)      echo "error msg"; exit
esac

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script for Parsing Log File Winsarc Shell Programming and Scripting 8 03-24-2011 11:12 AM
Parsing file in perl script maverick.usb Shell Programming and Scripting 2 04-09-2010 10:17 AM
parsing data for certain conditions PAW Shell Programming and Scripting 5 06-18-2009 09:10 AM
Script for parsing details in a log file to a seperate file pingnagan UNIX for Dummies Questions & Answers 2 11-13-2008 11:21 AM
Parsing a file in Shell Script sendhilmani123 Shell Programming and Scripting 4 11-30-2006 01:29 AM



All times are GMT -4. The time now is 10:19 PM.