LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-06-2003, 07:40 AM   #1
Tangerine
LQ Newbie
 
Registered: May 2003
Posts: 8

Rep: Reputation: 0
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
 
Old 05-06-2003, 08:11 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Please post where you got stuck.
 
Old 05-06-2003, 08:20 AM   #3
Tangerine
LQ Newbie
 
Registered: May 2003
Posts: 8

Original Poster
Rep: Reputation: 0
sorry i mean stuck in this problem..

dont know what the code is

but i found somewhere that the grep can help
 
Old 05-06-2003, 08:31 AM   #4
Tangerine
LQ Newbie
 
Registered: May 2003
Posts: 8

Original Poster
Rep: Reputation: 0
I DID IT WOHOOOOOOOOO

no need for help

thanks guys
 
Old 05-06-2003, 08:33 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.

Last edited by unSpawn; 05-06-2003 at 08:43 AM.
 
Old 05-06-2003, 12:00 PM   #6
Tangerine
LQ Newbie
 
Registered: May 2003
Posts: 8

Original Poster
Rep: Reputation: 0
WOW thanks but can u explaim the line of code for me?
 
Old 05-06-2003, 02:10 PM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash shell scripting Sco Linux - Newbie 1 11-09-2004 11:58 AM
Bash shell scripting question mehesque Programming 2 07-15-2004 10:54 AM
Help with I/O on bash shell scripting Dave6383 Programming 1 06-03-2004 05:24 PM
some bash shell scripting eltongeoff Linux - Newbie 2 10-22-2003 01:10 PM
BASH Shell scripting help ewarmour Linux - General 8 05-25-2002 07:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration