-1

So I´ve read several times about stdout, stdin and stderr. It sounds like a very simple concept but I don't know why I can't grasp it yet.

Well, I´m working on a script for homework. Part of it is that it will take at least two arguments. If it is less than two arguments, it should print an error on stderr, otherwise it should print the contents of all the files to stdout. (For now lets assume that all the files are text files, I don't want to deal with that yet)

So after reading Linux Pocket guide I have an idea of how to start

#!/bin/bash

if[$# -lt 2]
then
    echo $0 error: you must supply two arguments"
else
//stuff
fi

My first question is, the line of code:

echo $0 error: you must supply two arguments"

does that fulfill the requirement of "should print an error on stderr"?

Also, the homework is asking me to name the arguments a1, a2, a3...so on. How can I name the argument?

And last, how can I put everything together, in other words how can I concatenate the files and print them?

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
Haz
  • 41
  • 2
  • 7
  • 1
    Possible duplicate of http://stackoverflow.com/questions/2990414/echo-that-outputs-to-stderr – David Sep 16 '15 at 00:20
  • 1
    possible duplicate of [How to print on stderr without using /dev/stderr](http://stackoverflow.com/questions/32621828/how-to-print-on-stderr-without-using-dev-stderr) – Marc Young Sep 17 '15 at 10:14

1 Answers1

3

No.

echo $0

means to print the name of the file. You've not directed the output anywhere.

example:

#!/bin/bash
echo "name stdout: $0"
echo "name stderr: $0" >&2

&1 is a pointer to stdout. It can be not provided, it is the default &2 is a pointer to stderr

to prove this, run this script and redirect the scripts stderr output to /dev/null. If you see no output, then you know the echo was in stderr.

$ sh test.sh
  name stdout: test.sh
  name stderr: test.sh
$ sh test.sh 2>/dev/null #2 is stderror, redirect it to /dev/null
  name stdout: test.sh
$
Marc Young
  • 3,854
  • 3
  • 18
  • 22
  • Sorry it just doesn't make sense to me. I´m a beginner – Haz Sep 17 '15 at 01:56
  • You can still accept my answer since it's correct and the same as the one you accepted here: http://stackoverflow.com/questions/32621828/how-to-print-on-stderr-without-using-dev-stderr – Marc Young Sep 17 '15 at 10:14