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 ...
- 01-23-2008 #1Just 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 exitLast edited by devils casper; 01-24-2008 at 10:29 AM. Reason: Added [code].....[/code] tag.
- 01-23-2008 #2
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.
- 01-24-2008 #3Just Joined!
- Join Date
- Jan 2008
- Posts
- 2
I still don't get it.I wrote something else:
something must be done ,but what ?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
Last edited by devils casper; 01-24-2008 at 10:30 AM. Reason: Formatting removed and added [code].....[/code] tags.
- 01-26-2008 #4


Reply With Quote