Bash Shell Scripting Help
hi everyone
i want to prompt the user to enter a string such as hh i want code that looks for all the files in the current working directory with the string "hh" and then lists these files I just started shell scripting last week and i am stuck help appreciated :) |
Please post where you got stuck.
|
sorry i mean stuck in this problem..
dont know what the code is but i found somewhere that the grep can help |
I DID IT WOHOOOOOOOOO
no need for help thanks guys :) |
Ok. Try this:
#!/bin/sh # set -x echo "Enter a string to search for:"; read ans test -n "${ans}" && find . -type f | xargs -ix grep 'x' -e $ans *If you uncomment the "set -x" you'll get debug mode to show you how it works. Here's some scripting references: Introduction to BASH shell scripting: http://www.linuxorbit.com/modules.ph...icle&artid=459 A Quick Introduction to Bash Programming: http://www.geocities.com/tipsforlinu...cles2/043.html A's to Q's about BASH, the Bourne-Again SHell: http://apodeline.free.fr/FAQ/bashFAQ/bash.html BASH Programming - Introduction HOW-TO: http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html Advanced Bash Scripting Guide (ABS): http://www.tldp.org/LDP/abs/html/ Shell script resources: Heiner's SHELLdorado: http://www.shelldorado.com Linuxguru's shell scrip resources: http://www.linuxguruz.org/z.php?id=911 Working more productively with bash 2.x (Caliban.org, completion, good stuff): http://www.caliban.org/bash/ *Also search LQ for scripts and tips. |
WOW thanks but can u explaim the line of code for me?
|
read ans
Read user input (from stdin using a Bash builtin command (in bash: help read)) into a variable. test -n "${ans}" Test (man test) to see if the variable holds a non-zero* value. *Not a "real" check, shouldn't replace proper input validation. Beware what you're doing with unsanitized output like this. && find . -type f | Find in the current directory any item of type "file"* and pipe the output to xargs -ix grep 'x' -e $ans xargs which uses character "x" to represent the output from "find", which in turn is used by "grep" as the file(s) to search in. *Find uses "-type d" for dirs, but in essence a dir is a file too... |
All times are GMT -5. The time now is 03:07 PM. |