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 6914953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:24:50+00:00 2026-05-27T09:24:50+00:00

How can I write a shell script file_readable which: accepts some number of names

  • 0

How can I write a shell script file_readable which:

  • accepts some number of names as arguments,

  • checks each name to see if it is a regular file and readable, and

  • outputs a count of the number of such files.

For example:

$ sh file_readable /etc/fstab /etc/ssh/ssh_host_rsa_key /etc/does-not-exist
1

Of these, only /etc/fstab is likely to both exist and be readable.

So far I have put this together but it does not work correctly – can anybody help me please ?:

#!/bin/sh

for filename in "$@"

do
        if test -f "$filename"
        then  echo | wc -l
        else echo $?
fi
done
  • 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-27T09:24:51+00:00Added an answer on May 27, 2026 at 9:24 am

    then echo | wc -l

    If file exists and is a regular you print number of lines in empty string plus “\n”, which is equal 1 always. Sound not quite usable, isn’t it?

    All you need is incrementing some counter and printing it in the end.

    #!/bin/sh
    
    readable_files=0
    for filename in "$@"; do
        if test -f "$filename"; then
            readable_files=$(( readable_files + 1 ))
        fi
    done
    
    echo "${readable_files}"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I write a shell script that checks for an environment variable and
I want to write a shell script which will find the occurence of multiple
Assume I have some utility that can take a number of options, each followed
In SQL one can write a query that searches for a name of a
I'm trying to write a shell script to backup websites, I've got it to
I am trying to write a batch(for win) and a shell script for linux
I'm trying to write a Python script to test the output of some various
I currently have a request to build a shell script to get some data
I'm trying to write a shell script that does a search and replace inside
This is one of my homework exercise. Write a shell program, which will take

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.