Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Saturday, May 23, 2020

How to redirect the output of a command that reads a here document to another command in a shell script?

I wrote a long title here, "how to redirect the output of a command that reads a here document to another command in a shell script?"

I found this Stack Overflow post answers this question very well.
Following the answer over there, Below is an example,
{
 cut -d':' -f2 << END
A1: 3
A2: 4
A3: 1
A4: 3 
END
} | awk 'BEGIN {s=0;} {s+=$1;} END {print "sum =", s;}'

Friday, May 8, 2020

Running a Windows Command Line Job in Background

If we want to run a Windows command line task in background, like UNIX's "&" do, we can do the following from the Windows command line,

start /B cmd /C call program [arg1 [...]]

We explain each part as follows,
  • /B is the command line option to the start command. The documentation of the command start states the following,
    • /B. Start application without creating a new window. The application has ^C handling ignored. Unless the application enables ^C processing, ^Break is the only way to interrupt the application.
  • cmd is the program to run from the command line, i.e., C:\Windows\System32\cmd.exe
  • /C is the command line option given to cmd. The manual states,
    • /C. Carries out the command specified by string and then terminates
  • call is a command to cmd, it is to "call one batch program from another." The program does not have to be batch program, and can be any executable file.
  • program is the program to be called by the call command.
  • arg1 [...] are optional one or more command line arguments given to program.
For example, if we'd like to to run a Java program HelloWorld from the command line in background, we would do,

start /B cmd /C call java HelloWord

A Unix equivalent of this command would be java HelloWorld &.

Thursday, May 7, 2020

Removing Files Whose Names Start with "-"

I wanted to remove files whose names begin with "-" on a Linux system. After some searches, I found out that the following method worked.

rm -- -foo
where the file name is -foo.

Friday, September 13, 2013

Vim as Hex Editor


Vi Improved (vim) is a great tool. It can serve as a good Hex editor if the file you are viewing is not too big.

Once you open a file using Vim, use command %!xxd to enter hex mode. To exit hex mode, use %!xxd -r.