How to simulate "set -o vi" in csh , like we do in ksh? - command

I used ksh a lot but now moving to csh as this company has all scripts etc written in that.
One thing I loved about ksh was the ease of using the command history.
In case I have to edit the last command or second last command, I could easily press "Escape - k" to cycle through the commands and easily edit them.
csh does not seem to have a good equivalent. It displays all the history of commands then I have to copy paste one of them and then edit.
That's a pain when you want just change the number of in the file name for example :
cat abcdef1 | grep "Linking"

You could use (as a workaround):
"The most commonly used feature of csh command history is "!!" (pronounced "bang bang") which recalls the previous command":
% date
Mon Nov 25 13:52:36 PST 1996
% !!
date
Mon Nov 25 13:52:52 PST 1996
Here some other useful commands.
But you could just work in ksh and make script and whatever in csh.
If it is istalled simply launch:
/bin/ksh

Related

Using tac on most recent log file out of several log files in a directory

I have several log files in a directory that we’ll call path/to/directory that are in the following format after long listing in Red Hat Enterprise 6:
-rw-r——-. 1 root root 17096 Sep 30 11:00 logfile_YYYYDDMM_HHMMSS.log
There are several of these log files that are generated everyday. I need to automatically tac the most recently-modified file without typing the exact name of the log file. For example, I’d like to do:
tac /path/to/directory/logile*.log | grep -m 1 keyword
And have it automatically tac the most recently modified file and grep the keyword in the reverse direction from the end of the log file so it runs quicker. Is this possible?
The problem I’m running into is that there is always more than one log file in the /path/to/directory and I can’t get Linux to automatically tac the most recently modified file as of yet. Any help would be greatly appreciated.
I’ve tried:
tac /path/to/directory/logfile_$(date +%Y%m%d)*.log
which will tac a file created on the present date but the part that I’m having trouble with is using tac on the newest file (by YYYYMMDD AND HHMMSS) because multiple files can be generated on the same date but only one of them can be the most current and the most current log file is the only one I care about. I can’t use a symbolic link either.. Limitations, sigh.
The problem you seem to be expressing in your question isn't so much about tac, but rather .. how to select the most recent of a set of predictably named files in a directory.
If your filenames really are in the format logfile_YYYYDDMM_HHMMSS.log, then they will sort lexically without the need for an innate understanding of dates. Thus, if your shell is bash, you might:
shopt -s nullglob
for x in /path/to/logfile_*.log; do
[[ "$x" > "$file" ]] && file="$x"
done
The nullglob option tells bash to expand a glob matching no files as a null rather than as a literal string. Following the code above, you might want to test for the existence of $hit before feeding it to tac.

Format string on Linux and Solaris

I have a Korn Shell script, and one part of it is that it takes a given date in YYYYMMDD format and outputs it in YYYY/MM/DD format. At first I tried
typeset displaystart=`date --date="${gbegdate}" '+%Y/%m/%d'`
which works fine on Linux, but Solaris's date doesn't have a --date option. I then tried
typeset displaystart=`echo ${gbegdate:0:4}`/`echo ${gbegdate:4:2}`/`echo ${gbegdate:6:2}`
which also works on Linux, but on Solaris it just outputs //.
How can I format this date string in a way that works on Linux and Solaris?
The ${variable:start:length} extension to POSIX shell syntax was introduced in the version of ksh released in 1993, precisely named ksh93, and was also introduced in bash 1.13 the very same year.
The Advanced bash scripting guide from the Linux Documentation Project states:
Variable expansion / Substring replacement
These constructs have been adopted from ksh.
${var:pos}
Variable var expanded, starting from offset pos.
${var:pos:len}
Expansion to a max of len characters of variable var,
from offset pos. See Example A-13 for an example of the creative use
of this operator.
The issue is that on Solaris 10 and older, /bin/ksh is providing a previous ksh standard, ksh88, which didn't implemented this feature.
On the other hand, on Linux, ksh is often ksh93 which supports substring extraction. That explains why your script works under Linux ksh (if you really tested it on ksh.)
An old derivative of ksh93 is available on Solaris 10 though. It is named dtksh ans is located in /usr/dt/bin/dtksh. Your command should work unchanged with it however I wouldn't recommend to fully switch to dtksh, this shell being phased out from Solaris but you might still use it from a regular ksh script to workaround your issue:
typeset displaystart=$(/usr/dt/bin/dtksh -c "gbedate=$gbedate; echo \${gbegdate:0:4}/\${gbegdate:4:2}/\${gbegdate:6:2}")
Note that Solaris 11 and newer provide both GNU date and ksh93 so you wouldn't have that issue in the first place.
Korn shell doesn't have ${variable:start:length} syntax; this is a bash extension to POSIX shell syntax.
You can use echo "$variable" | cut -cstart-end instead.
typeset displaystart=`echo $gbegdate | cut -c1-4`/`echo $gbegdate | cut -c5-6`/`echo $gbegdate | cut -c7-8`
Or maybe you could change your script to use bash instead of ksh.

grep command to print follow-up lines after a match

how to use "grep" command to find a match and to print followup of 10 lines from the match. this i need to get some error statements from log files. (else need to download use match for log time and then copy the content). Instead of downloading bulk size files i need to run a command to get those number of lines.
A default install of Solaris 10 or 11 will have the /usr/sfw/bin file tree. Gnu grep - /usr/sfw/bin/ggrep is there. ggrep supports /usr/sfw/bin/ggrep -A 10 [pattern] [file] which does what you want.
Solaris 9 and older may not have it. Or your system may not have been a default install. Check.
Suppose, you have a file /etc/passwd and want to filter user "chetan"
Please try below command:
cat /etc/passwd | /usr/sfw/bin/ggrep -A 2 'chetan'
It will print the line with letter "chetan" and the next two lines as well.
-- Tested in Solaris 10 --

Need to convert ksh command line to sh

I'm trying to do a simple operation in ksh that I need to repeat in sh (Bourne shell)
All I want to do is append the contents of the first line of hte pay_period.txt file to the end of the new file name. This works great in ksh, but does not work in bourne. The program I'm using defaults to sh and I can't change that. Also I can't have actual shell scipts in the directories. So I have to issue commands.
How can I make the equivalent command below work in bourne
mv HEPAY.txt HE_PAY"$(/usr/bin/head -1 pay_period.txt)."txt
The results of $(/usr/bin/head -1 pay_period.txt) is 20140101.
If you are really talking about a real Bournce shell then you need to use backticks for command substitution ($() is POSIX and portable among "modern", POSIX-compliant shells but won't work in old, legacy shells), e.g.
mv HEPAY.txt HE_PAY`/usr/bin/head -1 pay_period.txt`.txt
Other than that I see no reason why this should not work.
PS: Note that head -1 isn't POSIX-compliant either (head -n 1 is).

How do I search a CVS repository for a particular file?

Is there any way to do it? I only have client access and no access to the server. Is there a command I've missed or some software that I can install locally that can connect and find a file by filename?
You could grep the output of
cvs rlog -Nh .
(note the period character at the end - this effectively means: the whole repository).
That should give you info about the whole shebang including removed files and files added on branches.
You can use
cvs rls -Rde <modulename>
which will give you all files in recursively, e.g.
foo:
/x.py/1.2/Mon Dec 1 23:33:51 2008//
/y.py/1.1/Mon Dec 1 23:33:31 2008//
D/bar////
foo/bar:
/xxx/1.1/Mon Dec 1 23:36:38 2008//
Notice that the -d option gives you also deleted files; not sure whether you
wanted that. Without -e, it only gives you the file names.