LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 06-30-2003, 05:40 AM   #1
aizkorri
Member
 
Registered: Feb 2002
Location: Basque Country
Distribution: Fedora 14, Ubuntu 14.04
Posts: 434
Blog Entries: 1

Rep: Reputation: 55
bash-script input


Hi, I've done a bash script that works with one input. like:

nireScript textMay6123.txt

I'm trying to execute that script with a * in the input so that It can be done for more than one file. e .g

nireScript textMay6*.txt

Do you know how can I read all that files as input from inside the script?
 
Old 06-30-2003, 10:28 AM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
When you put the '*' on the command line the shell will do the expansion before calling the script. When the script is called it may have more then one parameter. Take this example:

Code:
#!/bin/sh

# Script: test.sh

echo $1
echo $2
echo $3
Code:
# touch test1.txt
# touch test2.txt
# touch test3.txt

# ./test.sh test*.txt
test1.txt
test2.txt
test3.txt
The shell will find all the matches for test*.txt and substitute it for original text.
Code:
./test.sh test*.txt
./test.sh test1.txt test2.txt test3.txt
So this script should work for any number of parameters:
Code:
#!/bin/sh

for i in $@; do
  echo $i
done
 
Old 07-01-2003, 01:13 AM   #3
aizkorri
Member
 
Registered: Feb 2002
Location: Basque Country
Distribution: Fedora 14, Ubuntu 14.04
Posts: 434

Original Poster
Blog Entries: 1

Rep: Reputation: 55
understood, thanks a lot.
 
Old 07-07-2003, 07:20 AM   #4
yuray
Member
 
Registered: Apr 2003
Location: Russia, Khotkovo
Distribution: Debian
Posts: 146

Rep: Reputation: 15
#!/bin/sh

for i
echo $i
done

From book Kernigan & Pike

 
Old 07-07-2003, 07:22 AM   #5
yuray
Member
 
Registered: Apr 2003
Location: Russia, Khotkovo
Distribution: Debian
Posts: 146

Rep: Reputation: 15
Sorry

for i
do
echo $i
done
 
Old 07-07-2003, 08:27 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Kinda OT note: make sure you use proper quoting.
touch "vermin0.txt;ls -al" "vermin1.txt *" vermin2.txt
bad() { set -x; for i in $@; do echo "got: $i"; done; set +x; }
good() { set -x; IFS="
" ; for i in "$@"; do echo ""$i""; done; set +x; }

Now try "bad verm*" and you'll see why.
 
Old 07-08-2003, 12:11 AM   #7
yuray
Member
 
Registered: Apr 2003
Location: Russia, Khotkovo
Distribution: Debian
Posts: 146

Rep: Reputation: 15
Kernighan can`t be wrong

yuray@yuray:~/ttt$ bad() { set -x; for i; do echo "got: $i"; done; set +x; }
yuray@yuray:~/ttt$ bad verm*
+ echo 'got: vermin0.txt;ls -al'
got: vermin0.txt;ls -al
+ echo 'got: vermin1.txt *'
got: vermin1.txt *
+ echo 'got: vermin2.txt'
got: vermin2.txt
+ set +x
yuray@yuray:~/ttt$
 
Old 07-08-2003, 06:15 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Got no beef with Kernighan. You didn't see me spice it up with "$@": bad() { set -x; for i in $@; do (...)
 
  


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
Shell script pipe input - bash mostly laikos Programming 4 11-09-2008 05:14 PM
mask user input in a bash script PlatinumRik Linux - Software 1 06-15-2004 10:06 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script to accept input ONLY until background process completes andrewstr Linux - Software 2 03-17-2004 12:02 PM
User input using a BASH script... causticmtl Programming 5 07-13-2003 09:59 PM

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

All times are GMT -5. The time now is 01:03 PM.

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