For Unix Users

Horizontal Rule [MAR 97]

Sandie Hui

Some Unix tips were presented in the last few issues. More will be given in the following section to provide novice users with information on navigating the operating system. So, have fun!



Q. How do I read characters from the terminal in a shell script?

A. In sh, use read. It is most common to use a loop like

            while read line
            do
                    ...
            done
In csh, use $< like this:
 
      while ( 1 )
		set line = "$<"
		if ( "$line" == "" ) break
                ...
        end

Unfortunately csh has no way of distinguishing between a blank line and an end-of-file.

If you're using sh and want to read a *single* character from the terminal, you can try something like

    echo -n "Enter a character: "
	stty cbreak         			# or  stty raw
	readchar='dd if=/dev/tty bs=1 count=1 2>/dev/null'
	stty -cbreak
 	echo "Thank you for typing a $readchar ."



Q. How do I change file names to lowercase?

A. You could use something like this to rename uppercase files to lowercase:

C Shell:

	foreach f ( * )
		mv $f 'echo $f | tr '[A-Z]' '[a-z]''
	end
Bourne Shell:
	for f in *; do
		mv $f 'echo $f | tr '[A-Z]' '[a-z]''
	done

If you wanted to be really thorough and handle files with 'funny' names (embedded blanks or whatever) you'd need to use

Bourne Shell:

	for f in *; do
		g='expr "xxx$f" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]''
		mv "$f" "$g"
	done
The 'expr' command will always print the filename, even if it equals '-n' or if it contains a System V escape sequence like '\c'.




Q. How do I redirect stdout and stderr separately in csh?

A. In csh, you can redirect stdout with ">", or stdout and stderr together with ">&" but there is no direct way to redirect stderr only. The best you can do is

        ( command >stdout_file ) >&stderr_file
 
which runs "command" in a subshell; stdout is redirected inside the subshell to stdout_file, and both stdout and stderr from the subshell are redirected to stderr_file, but by this point stdout has already been redirected so only stderr actually winds up in stderr_file.

If what you want is to avoid redirecting stdout at all, let sh do it for you.

        sh -c 'command 2>stderr_file'



Q. How do I ring the terminal bell during a shell script?

A. The answer depends on your Unix version (or rather on the kind of "echo" program that is available on your machine).

A BSD-like "echo" uses the "-n" option for suppressing the final newline and does not understand the octal \nnn notation. Thus the command is

        echo -n '^G'
 
where ^G means a _literal_ BEL-character (you can produce this in emacs using "Ctrl-Q Ctrl-G" and in vi using "Ctrl-V Ctrl-G").

A SysV-like "echo" understands the \nnn notation and uses \c to suppress the final newline, so the answer is:

        echo '\007\c'


[Issue No. 10]


[u logo]
Computing Services Centre
City University of Hong Kong
ccnetcom@cityu.edu.hk

[Home Page][CSC Home][NetComp Home][Content Home][Previous Page][Next Page]