cat command is not working inside shell script
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 \;
)
Related videos on Youtube
Ehsan Sajjad
Updated on September 18, 2022Comments
-
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 almost 10 yearsYou 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 almost 10 yearsYou should write the logic inside a filter instead.
-
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 almost 10 yearsYou can register the filter as global if you need it to be applied to all controllers.
-
Ehsan Sajjad almost 10 yearscan you elaborate with some sample?
-
roaima over 8 yearsWhat 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 over 8 yearsIf it hangs, then the output of
find /home/peter/databases -name "cells.txt"
is empty in the ksh script. -
schily over 8 yearsWhat happens when you write
/bin/cat
? Note thatcat
is aksh
builtin. -
SS Hegde over 8 yearsThere 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 over 8 years@SSHegde What's the output of
echo `find /home/peter/databases -name "cells.txt"`
in the script? -
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 over 8 yearsOh. So the shell is taking time to build the argument list using the output of
find
, which you say is nearly a thousand files. Usefind
's-exec
withcat
, instead. -
wurtel over 8 yearsRun 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 thefind
step. -
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 almost 10 yearswhat about overriding built-in filter
-
Khanh TO almost 10 years@Ehsan Sajjad: about overriding, there is already a stackoverflow answer: stackoverflow.com/questions/19358802/…
-
Khanh TO almost 10 years@Ehsan Sajjad: I usually just use the built-in filter without modification.
-
Ehsan Sajjad almost 10 yearsbut this way i have to decorate actions with that attribute
-
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 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 over 8 yearsThe solution works perfectly fine. Thank you. But why not with my way? What was wrong? :o(
-
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 tocat
files whose names contained a space. -
SS Hegde over 8 yearsplease see my EDIT 4
-
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.