Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7194093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:18:07+00:00 2026-05-28T20:18:07+00:00

n00b question alert! here is the problem: I am creating a shell script that

  • 0

n00b question alert!
here is the problem:
I am creating a shell script that takes a minimum of 3 arguments: a string, a line number, and at least one file.

I’ve written a script that will accept EXACTLY 3 arguments, but I don’t know how to handle multiple file name arguments.

here’s the relevant parts of my code (skipping the writing back into the file etc):

#!/usr/bin/env ruby

the_string = ARGV[0] 
line_number = ARGV[1]
the_file = ARGV[2] 

def insert_script(str, line_n, file)
  f = file
  s = str
  ln = line_n.to_i

  if (File.file? f)
    read_in(f,ln,s)
  else 
    puts "false"
  end
end

def read_in(f,ln,s)
  lines = File.readlines(f)
  lines[ln] = s + "\n"
  return lines
end

# run it
puts insert_script(the_string, line_number, the_file)

now I know that it’s easy to write a block that will iterate through ALL the arguments:

ARGV.each do |a|
   puts a 
end

but I need to ONLY loop through the args from ARGV[2] (the first file name) to the last file name.

I know there’s got to be – at a minimum – at least one easy way to do this, but I just can’t see what it is at the moment!

in any case – I’d be more than happy if someone can just point me to a tutorial or an example, I’m sure there are plenty out there – but I can’t seem to find them.

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:18:08+00:00Added an answer on May 28, 2026 at 8:18 pm

    If you modify the ARGV array to remove the elements you’re no longer interested in treating as filenames, you can treat all remaining elements as filenames and iterate over their contents with ARGF.

    That’s a mouthful, a small example will demonstrate it more easily:

    argf.rb:

    #!/usr/bin/ruby
    
    str = ARGV.shift
    line = ARGV.shift
    
    ARGF.each do |f|
        puts f
    end
    
    $ ./argf.rb one two argf.rb argf.rb 
    #!/usr/bin/ruby
    
    str = ARGV.shift
    line = ARGV.shift
    
    ARGF.each do |f|
        puts f
    end
    #!/usr/bin/ruby
    
    str = ARGV.shift
    line = ARGV.shift
    
    ARGF.each do |f|
        puts f
    end
    $ 
    

    There are two copies of the argf.rb file printed to the console because I gave the filename argf.rb twice on the command line. It was opened and iterated over once for each mention.

    If you want to operate on the files as files, rather than read their contents, you can simply modify the ARGV array and then use the remaining elements directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm on a roll today. Here goes n00b question number 7: What's the difference
Call me a 'n00b', but I am new to creating Script Controls. I want
maybe this is a n00b-question. I try to parse an xml-file like that: <?xml
total n00b question here but I can't seem to find the answer searching around
n00b here (first Android project). I have been given a custom video codec that
Total n00b question here. I am using pgAdmin to attempt to administer a remote
This feels like an extremely n00b question, but here goes... I have a series
i am not sure if its a total n00b question but here goes. i
Here's a question from a Crystal n00b. Accessing a database using JDBC and using
I'm sure this sounds like a n00b question, but how do I add sub

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.