Find the answer to your Linux question:
Results 1 to 4 of 4
I wrote this script , but it didn't work.It must find a specific example in a specific file using grep command entered by the user,but the file and example are ...
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    2

    UNIX script help

    I wrote this script , but it didn't work.It must find a specific example in a specific file using grep command entered by the user,but the file and example are like variables entered in the shell promt,not declared in the script.
    it must look like this ./scriptname 'word or something' filename
    Code:
    #!/bin/bash 
    OK=0 
       FILENAME=$1 
       WORD=$* 
    if [ $? -eq $OK ] 
    then 
    grep $FILENAME | while read $FILENAME 
       do 
          grep -in "$WORD"   $FILENAME 
    done 
    else 
    echo "$WORD not found in $FILENAME" 
    fi 
    exit
    Last edited by devils casper; 01-24-2008 at 10:29 AM. Reason: Added [code].....[/code] tag.

  2. #2
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    There are some obvious problems with the script. To spot them, run it with the debugger, e.g.:
    bash -x ./script_here arg1 arg2

    You can watch what values are really getting assigned to variables and troubleshoot from there.

    If you don't already know about it, the Advanced Bash Scripting Guide on tldp is a good reference.

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Posts
    2
    I still don't get it.I wrote something else:
    Code:
    #!/bin/bash
    OK=0
    word=''
    filename=''
    for filename
    do grep -in "$word" "$filename"
    
    if [ $? -eq $OK ]
    then
    echo "$word found in $filename"
    else
    echo "$word not found in $filename"
    fi
    done
    exit 
    and the debugger said:
     bash -x ./new.txt 'just' news.txt
    + OK=0
    + word=
    + filename=
    + for filename in '"$@"'
    + grep -in '$word' '$filename'
    grep: $filename: No such file or directory
    + '[' 2 -eq 0 ']'
    + echo ' not found in just'
     not found in just
    + for filename in '"$@"'
    + grep -in '$word' '$filename'
    grep: $filename: No such file or directory
    + '[' 2 -eq 0 ']'
    + echo ' not found in news.txt'
     not found in news.txt
    + 'exit$'
    ./new.txt: line 14: exit$: command not found
    something must be done ,but what ?
    Last edited by devils casper; 01-24-2008 at 10:30 AM. Reason: Formatting removed and added [code].....[/code] tags.

  4. #4
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    You still need to read the scripting guide I pointed you to. Particularly the section about positional parameters.

    Using the debugger to figure out what's happening with your script at runtime is a good practice.

Posting Permissions

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