cat command is not working inside shell script

3,144

Solution 1

The script worked by ignoring the file sizes of zero in find command. (-size +0)

 cat `find /home/peter/databases -name "cells.txt" -size +0`

I agree with the other solution also. But since my question was to find the reason why the ksh is unable to process the cat was because of the buffer size (output of back quote), i am posting my answer. @wurtel: Thank you. your suggestion solved the issue.

Solution 2

Try this as an alternative solution, which will invoke cat only when it finds a file

find /home/peter/databases -type f -name 'cells.txt' -exec cat {} +

(If your version of find does not understand the trailing +, replace it with the two characters \;)

Share:
3,144

Related videos on Youtube

Ehsan Sajjad
Author by

Ehsan Sajjad

Updated on September 18, 2022

Comments

  • Ehsan Sajjad
    Ehsan Sajjad over 1 year

    I have created my own controller in which i am checking for each request if Session exists execute action otherwise redirect user for login.

    Here is my code:

    public class DefaultController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.HttpContext.Session["User"] == null)
            {
                filterContext.Result = RedirectToAction("Login", "Account");
            }
            else
            {
                base.Execute(filterContext.RequestContext);
            }
        }
    }
    

    and i am using it in my controller:

    public class HomeController : DefaultController
    {
        public ActionResult Index()
        {
            // DXCOMMENT: Pass a data model for GridView
            return View();    
        }
    }
    

    when i go to Home/Index it is redirecting me to lo-gin according to logic but if use is already logged in (Session exists) it is throwing exception instead of executing that action.

    Here is the exception:

    A single instance of controller cannot be used to handle multiple requests. If a custom controller factory is in use, make sure that it creates a new instance of the controller for each request.

    • Brunis
      Brunis almost 10 years
      You could use async await to execute each action in it's own thread. If you have the latest VS and .NET, try adding a new MVC/WebAPI project, the new sample projects default to using async/await. Hope that helps.
    • Khanh TO
      Khanh TO almost 10 years
      You should write the logic inside a filter instead.
    • Ehsan Sajjad
      Ehsan Sajjad almost 10 years
      @KhanhTO in that case i will have to decorate each action explicitly for Session check i need more generic so i am doing this way
    • Khanh TO
      Khanh TO almost 10 years
      You can register the filter as global if you need it to be applied to all controllers.
    • Ehsan Sajjad
      Ehsan Sajjad almost 10 years
      can you elaborate with some sample?
    • roaima
      roaima over 8 years
      What happens when it "doesn't work"? Error messages? Unexpected output? No output? Please be precise (and edit the answers into your question for all to see.)
    • muru
      muru over 8 years
      If it hangs, then the output of find /home/peter/databases -name "cells.txt" is empty in the ksh script.
    • schily
      schily over 8 years
      What happens when you write /bin/cat? Note that catis a ksh builtin.
    • SS Hegde
      SS Hegde over 8 years
      There are nearly 1000 files listed with find command. But most of them are empty. The above said cat command is working in sh but not in ksh.
    • muru
      muru over 8 years
      @SSHegde What's the output of echo `find /home/peter/databases -name "cells.txt"` in the script?
    • SS Hegde
      SS Hegde over 8 years
      @muru: the echo is hanging. i mean the echo is not giving any output neither it is giving me the next command prompt.
    • muru
      muru over 8 years
      Oh. So the shell is taking time to build the argument list using the output of find, which you say is nearly a thousand files. Use find's -exec with cat, instead.
    • wurtel
      wurtel over 8 years
      Run the ksh script as ksh -x scriptname and see what it's actually executing. Also it might be useful to add -size +0 to skip empty files at the find step.
    • SS Hegde
      SS Hegde over 8 years
      @wurtel: skipping the empty files worked !! Thank you.I will add that option and continue. But what could be the problem without adding that? Any memory constraint with find?
  • Ehsan Sajjad
    Ehsan Sajjad almost 10 years
    what about overriding built-in filter
  • Khanh TO
    Khanh TO almost 10 years
    @Ehsan Sajjad: about overriding, there is already a stackoverflow answer: stackoverflow.com/questions/19358802/…
  • Khanh TO
    Khanh TO almost 10 years
    @Ehsan Sajjad: I usually just use the built-in filter without modification.
  • Ehsan Sajjad
    Ehsan Sajjad almost 10 years
    but this way i have to decorate actions with that attribute
  • Khanh TO
    Khanh TO almost 10 years
    @Ehsan Sajjad: by registering the filter as global, it will be applied to all controllers. You don't need to decorate manually.
  • Khanh TO
    Khanh TO almost 10 years
    @Ehsan Sajjad: In case you need more fine-grained control like AllowAnonymous in your custom filter, you have to implement more logic in your custom filter to check for the existence of this attribute.
  • SS Hegde
    SS Hegde over 8 years
    The solution works perfectly fine. Thank you. But why not with my way? What was wrong? :o(
  • roaima
    roaima over 8 years
    @SSHegde for your version if there were no files found, the cat would run with no list of files, default to reading stdin, and wait to copy whatever you typed. Your version would also fail to cat files whose names contained a space.
  • SS Hegde
    SS Hegde over 8 years
    please see my EDIT 4
  • roaima
    roaima over 8 years
    @SSHegde different shells may have different buffer lengths for long command lines. It seems that for one shell you can fit all the filenames, but for the other it overflows.