-4

What's going on everyone? I have this assignment in my Linux Operating System class and im having a little trouble. It asks me to,

Write a Bash shell script called chkread that takes an unlimited number of arguments that all represent file names.

I have come up with this so far, but i don't think it's exactly what the professor is looking for.

~$ cat MY_SCRIPT
#!/bin/bash
echo ${unlimited arguments}
~$ bash MY_SCRIPT cat dog horse
cat dog horse
5
  • No, it's definitely not. You also might be confusing StackOverflow, a Q&A site, with a forum, which it's not. But we do have some nice chat rooms once you've gained enough rep :) Commented Apr 5, 2016 at 19:35
  • ah, sorry about that. i classmate had recommended this site to me and i thought it was just that. thank you though! Commented Apr 5, 2016 at 19:52
  • We're more than happy to answer questions, but we do ask for questions that can be answered. Spend a few minutes reading/re-reading the FAQ, take a look at catb.org/esr/faqs/smart-questions.html and stackoverflow.com/help/mcve and you'll find that we are an extremely helpful bunch here, at least if you ask good questions ;) Typically a good question will contain 1) what do you want to accomplish? 2) What have you tried to solve your problem? If you've searched, what have you searched for? If you wrote code, paste the MCVE. 3) What did you want to happen? Commented Apr 5, 2016 at 20:01
  • 4) What happened instead? If you got an error, post the full error. "it didn't work" is never a complete explanation. Commented Apr 5, 2016 at 20:02
  • Assignment questions have typically been asked many times before. Google before asking here. Commented Apr 7, 2016 at 5:35

2 Answers 2

1

You could use something like this to handle multiple arguments in a bash script.

#!/bin/bash
file_names=("$@")

for name in "${file_names[@]}"; do
  echo "$name"
done

And then when you call the script:

bash chkread.sh file1 file2 file3 file4

The script will print them to output:

file1
file2
file3
file4

But this is just an example. Inside the script you can do with them whatever you need to do.

Sign up to request clarification or add additional context in comments.

Comments

-2

Use $*. That will give you everything. For example:

#!/bin/bash
file_names=$*
echo $file_names

Output:

jbanks@efsappdev1:~$ x.sh `ls *.sql`
current.sql goop.sql latest.sql long.sql report.sql
jbanks@efsappdev1:~$ x.sh one two three
one two three

4 Comments

You're right, I should have used proper formatting. Sorry for doing that.
This doesn't work with file names containing whitespace.
True. Depending on OS it probably work either. You can also escape the whitespace, but it's not really easy without invoking another process, I don't think. Obviously, something like this lends itself to perl or such, but the OP was directed to use the wrong tool for the job (IMO), so that kinda restricts things.
Not at all. Bash can handle this just fine. See the other answer with "$@".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.