background preloader

Bash

Facebook Twitter

Cod

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. 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 [ steve-parker.org ]

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. 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. This is done by giving the list a name in the file system and then substituting that name in the command line. 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 For example, given the two files: set +o posix. Discover tput. What is 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. Several commonly used programs in UNIX rely on the terminfo database for these attributes as well as many others, including the vi and emacs editors as well as the curses and man programs.

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 Moving the cursor and displaying information #!