Hi
Can anyone explain me the following command.
This may be a minor one, but, please do explain in detail.

CHKTRANS=`echo ${0} | grep '/transforms/' | wc -l`

Recommended Answers

All 10 Replies

It greps whether the command used contains the string "/transforms/" and returns 1 if it does and 0 if it doesn't.

$0 (or, iow, ${0}) refers to the actual command used on the command line.

$0 is used to refer file-name.

My question is "/transforms/" is any predefined command or a word "/transforms/" in my file..?

No, that is just looking for that EXACT string in the provided argument (not in the contents of the file it references).

ok thank you

In the same way can you tell me the difference between $0 and ${0} ?

A variable can be referred to as either $VARNAME or ${VARNAME}. The latter form is useful if the variable name is followed by something that's not supposed to be part of the variable name. For example say you have a variable FOO and you want to print the contents of the variable followed by the string "_BAR". If you write echo $FOO_BAR it will look for a variable named FOO_BAR. So you write echo ${FOO}_BAR instead.

In this case there's a space directly after the ${0} so the braces aren't actually necessary. So in this case there is no difference between the two.

On my system, the 'transforms' command doesn't exist, so the output of this command is always '0'. I looked in all of my application repositories, and could not find it - even trying to install what I thought might be relevant packages. Still no luck! Anyway, let's look at the processes in detail.

  1. The command 'echo ${0}' returns the command name that executed - in this case, 'bash' - the shell command that I am running.
  2. That is piped into the command "grep '/transforms/'" - which due to the path specified, must exist in the / (root) directory of your system - unlikely...
  3. The output of that is piped to the 'wc -l' command which would return the number of lines from the '/transforms/' command. Since that (/transforms/) doesn't exist, the output is '0'.

So, I have to ask, what EXACTLY are you trying to do?

The command 'echo ${0}' returns the command name that executed - in this case, 'bash' - the shell command that I am running.

Since the code was almost certainly part of a shell script, not typed directly into the shell, $0 would be the name (and path) of the script file, not the name of the shell.

That is piped into the command "grep '/transforms/'" - which due to the path specified, must exist in the / (root) directory of your system - unlikely...

What? grep 'transform' runs the executable grep with the argument /transform/. grep must be located somehwere in your $PATH (most likely /bin) and, unless you have a completely non-standard system, it will.

The output of that is piped to the 'wc -l' command which would return the number of lines from the '/transforms/' command. Since that (/transforms/) doesn't exist, the output is '0'.

It produces the number of lines in the output of the grep command. And as masijade already pointed out, grep will produce one line of output if $0 contained the substring /transform/ (which will be the case if the script is placed inside of a directory named transform or anywhere below (i.e. in a sub-directory, sub-sub-directorc etc. of) it. Otherwise it will produce no output.

Thanks, but i am not clear differrence between this two sum.

I'm afraid I do not understand your question. None of the code posted in this thread contains any sums.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.