background preloader


Facebook Twitter


Bourne. Determine If Shell Input is Coming From the Terminal or From a Pipe. Working on a little script the other day I had the need to determine if the input to the script was coming from a pipe or from the terminal.

Determine If Shell Input is Coming From the Terminal or From a Pipe

Seems like a simple enough thing to determine but nothing jumped immediately to mind and a quick internet search didn't help much either. After a bit of pondering I came up with two solutions: the stat command and using information from the proc file system. The first solution uses the stat command to determine what type of file is connected to standard input. First we find out what is connected to standard input: stdin="$(ls -l /dev/fd/0)"stdin="${stdin/*-> /}" The file /dev/fd/0 is the standard input, which is a symbolic link. Now we use stat to get the file type: ftype="$(stat --printf=%F $stdin)" Then we just test the file type: if [[ "$ftype" == 'character special file' ]]; then echo Terminal elif [[ "$ftype" == 'regular file' ]]; then echo Pipe: $stdinelse echo Unknown: $stdinfi We can test it via: We test this the same way:

UNIX BASH scripting. UNIX / Linux Bourne / Bash Shell Scripting Tutorial [ ] Bashfully Yours, Gem Shortcuts. Bash Process Substitution. In addition to the fairly common forms of input/output redirection the shell recognizes something called process substitution.

Bash Process Substitution

Although not documented as a form of input/output redirection, its syntax and its effects are similar. The syntax for process substitution is: <(list) or >(list) where each list is a command or a pipeline of commands. The effect of process substitution is to make each list act like a file. To substitute a command pipeline for an input file the syntax is: command ... To substitute a command pipeline for an output file the syntax is: command ...

At first process substitution may seem rather pointless, for example you might imagine something simple like: uniq <(sort a) to sort a file and then find the unique lines in it, but this is more commonly (and more conveniently) written as: sort a | uniq The power of process substitution comes when you have multiple command pipelines that you want to connect to a single command. For example, given the two files: set +o posix. Discover tput. What is tput?

Discover tput

The tput command initializes and manipulates your terminal session through the terminfo database. Using tput, you can alter several terminal capabilities, such as moving or altering the cursor, changing text properties, and clearing specific areas of the terminal screen. Back to top What is the terminfo database? The terminfo database on a UNIX system defines terminal and printer attributes and capabilities, including the number of lines and columns for the respective device (for example, terminal and printer) and attributes of text to be sent to the device. Command-line introduction to tput The tput command, like most commands in UNIX, can be used either at your shell command line or inside a shell script.

Cursor attributes Moving the cursor or altering its attributes can be helpful in UNIX shell scripts or at the command line. Moving the cursor Moving the cursor’s position on the respective device is easily done with tput.