LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 06:40 AM   #1
aizkorri
Member
 
Registered: Feb 2002
Location: Basque Country
Distribution: Fedora 14, Ubuntu 14.04
Posts: 425
Blog Entries: 1

Rep: Reputation: 36
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, 11:28 AM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
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, 02:13 AM   #3
aizkorri
Member
 
Registered: Feb 2002
Location: Basque Country
Distribution: Fedora 14, Ubuntu 14.04
Posts: 425
Blog Entries: 1

Original Poster
Rep: Reputation: 36
understood, thanks a lot.
 
Old 07-07-2003, 08: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, 08: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, 09:27 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,814
Blog Entries: 54

Rep: Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989Reputation: 2989
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, 01: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, 07:15 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,814
Blog Entries: 54

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


All times are GMT -5. The time now is 10:29 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration