Today I have been working on a script to execute to view entries within a log file.
I have successfully got the command I want to execute within the script itself. I want to view the last 5 entries within a log file and see just the last numbers. The file name would change depending on the date.
Returns what I expect to see
However I want to execute this within a shell script and the file is located within a different location to the log file.
I tried to setup an alias within the shell script but that hasn't worked so I am not 100% sure how to do this.
Any help would be much appreciated.
The log file location: /opt/product/apachetomcat/8080/8.5.5_8080/apache-tomcat-8.5.5/logs
I am not sure but probably the various nested subshells are at fault. As Corona688 mentioned. I only saw your second version and did not realise in your first posting the "cd" was within the assignment. So let us go over your code:
First: you should NOT use the cd-command in a script. Never! Rather than
do it like this:
or, better yet (especially if you need the path several times, not once), like this:
Next: the various pipelines. Always be aware that calling a program takes time: to load the bnary, then to set up and start the sub-process, then to process the data and then remove the ended subprocess. Usually processing the data is the smallest part of this. Avoid these subproccesses.
Let us have a look:
this:
will do what? First, it deletes some lines with HTTP... in them, then takes the first remaining one, the filters out all lines with POST in them (there is only one line at that time) and then trims this line (if any). All of this can be done in a single invocation of sed:
This is equivalent to your first sed-invocation plus the pipeline to head: it waits for the first line NOT containing "HTTP...", prints it and quits. All other lines are silently dropped because of the "-n". That means, only one line will be printed. I also removed the extended regexps. Only GNU-sed understands them since they are not POSIX and it is good practice to write scripts as portable as possible. Now the rest. I use another rule for lines containing "POST", which trims them and then prints them out. Regardless of something being printed or not, sed quits after this, so either a line with POST is printed or nothing:
Now for the rest of the command:
Do you really want to access the file created last or could it be the file last modified? Maybe it won't matter because logfiles are usually created, written to, then the next one is created, etc. The sorting for modification time and the one for creation time would produce the same results in this case. In any way you should NOT use backticks any more. You used "$(...)" within, why not use them outside too?
And, finally, make your code better readable. Lines are free of charge, so you don't have to press everything into one line:
It might be only me, but i find this easier to comprehend what belongs to where. Perhaps even better:
When I use this command:
vi /home/bob/.vimI expect to see.
" ============================================================================
" Netrw Directory Listing (netrw v149)
" /home/bob/.vim
" Sorted by name
" Sort sequence:... (4 Replies)
How can view log messages between two time frame from /var/log/message or any type of log files.
when logfiles are very big and especially many messages with in few minutes, I would like to display log messages between 5 minute interval.
Could you pls give me the command? (1 Reply)
Hi.,
Last modified time of the folder is changing when I view the file inside the directory. Here is the test on sample directory. I believe that ls -l commands gives the time detail w.r.t last modified time. Pl. suggest.
bash-3.2$ mkdir test
bash-3.2$ cd test
bash-3.2$ touch myfile.txt... (2 Replies)
Hi Everybody,
I am Unable to view files in a particular directory under /opt. But, when I reboot the server, I am able to view the files.. Its happening daily. Do u 've n e answers/suggestions.
Kindly help..
:eek: (1 Reply)
Hi Everybody,
I am Unable to view files in a particular directory under /opt. But, when I reboot the server, I am able to view the files.. Its happening daily. Do u 've n e answers/suggestions.
Kindly help..
:confused: (1 Reply)
Hi,
The app I support has a directory used for debugg files. It has just come to light there are no files in there. Or is there :confused:
Changing into the directory and completing an ls -lta you get no files. This is truly not right for the application. If I go up one directory the... (2 Replies)
Hello, and thanks in advance-
I just installled red hat and pinged my machine and got a reply. When i go to http://myIPhere from my other machine it asks for a password and username. It doesnt accepts the username and passwords I use to login to my linux box. I therefore think its a premissions... (1 Reply)
Hi, first post here be gentle. Very new to Unix. Using HP-UX 10.20
I CD into a remote directory on one machine
$ cd /net/remote hostname
yet when I do an ll in this directory none of the contents appear. It just is empty.
when I do the same command from another machine,
$ cd... (13 Replies)