Sponsored Content
Top Forums UNIX for Dummies Questions & Answers exec command and field descriptors.. Post 58924 by Perderabo on Saturday 4th of December 2004 04:02:16 PM
Old 12-04-2004
The trouble is that exec can do several different things...it really needs to be several commands. The code you gave won't work. Change that to
exec $fd < inputfile
and it probably will. But that is not what's meant by an fd.

A fd is always an integer. In shell scripts, it will be a very low integer. By convention:
0 = standard input
1 = standard output
2 = standard error output

The idea is that you write your program to output to fd 1 without knowing what fd one is. Then at execution time you can do stuff like:
echo this > first.file
echo that > second.file

It would be terrible if echo always sent stuff to "first.file". You would need to do:
echo that
cp first.file second.file
or something like that.

By default 0 1 2 are all connected to /dev/tty so you can type input to a program and see the results in your window.

Here is an experiment I just did:
$ expr 1 + 2
3
$ expr 1 + 2 > expr.out
$ cat expr.out
3
$ expr cat + dog > expr.out
expr: non-numeric argument
$

With the last expr command, I have an error. Since the error goes to 2 which is still /dev/tty, I see it immediately, even though the standard which is 1 goes to a file. That why we have both 1 and 2. You can send 1 into a file while 2 is still displayed to you.

Don't want to see error messages? Bad idea usually, but you can do:
expr cat + dog > expr.out 2>/dev/null

And now error messages are thrown away.

expr cat + dog > expr.out
really means
expr cat + dog 1> expr.out
but if you leave the integer off, 1 is assumed for > while 0 is assumed for <
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

using the -exec command

linux redhat 8.0 I am getting accustomed to using the -exec command to get around my databse.. and use it to edit and update files..! is this more apllicable than jumping from one directory to the other.. I have set up the databse so that the inode #'s are accessable and can get me from one... (0 Replies)
Discussion started by: moxxx68
0 Replies

2. Shell Programming and Scripting

Help with exec command and file descriptors??

im having trouble with the comprehending the exec command and the use of file descriptors. given: #!/bin/sh exec 4>&1 exec 1>&2 exec 2>&4 exec 4>&- echo Hello would the standard output of this script be sent to STDOUT, STDERR or neither and why??? thanks for the help. (1 Reply)
Discussion started by: rfourn
1 Replies

3. Shell Programming and Scripting

Help with exec command and file descriptors II

Need to close files which descriptor number are larger than 9 in ksh. 'exec 10>&-' fails with 'ksh: 10: not found'. How do you specify file descriptors which occupies two or more digits in ksh script? Thanks, Masaki (2 Replies)
Discussion started by: masaki
2 Replies

4. Shell Programming and Scripting

exec command

can any one pls explain the meaning of exec 1<&5 ?? its urgent (2 Replies)
Discussion started by: santosh1234
2 Replies

5. HP-UX

exec and file descriptors

Hi, I speak and write english more or less, so I hope my asking be clear. :) In the company I am working, they are using control-m software to lunch shell scripts. So i put this command in all shell scripts: export LOGFILE_tmp=$PRODUC_DATA/tmp/${SCRIPT}_${PAIS}_`date... (0 Replies)
Discussion started by: anamcara
0 Replies

6. UNIX for Dummies Questions & Answers

Exec command

Hi can some one explain the following command , It would really help if some can really elloborate on what is happening out here export PATH | exec /bin/sh ./auto_approve :q P.S: This is the first time i am using exec ,so an elloboration what does it do and what is the use of the :q will be... (1 Reply)
Discussion started by: Sri3001
1 Replies

7. Shell Programming and Scripting

exec command help

Hi, I have the following lines in a script : . . exec < some_file . . . I have very little idea about exec command. I would like to know what this does and what will happen if the file some_file does not exist. Specifically, I would like to know whether the lines following this... (5 Replies)
Discussion started by: elixir_sinari
5 Replies

8. Shell Programming and Scripting

Use 3 descriptors with ssh command

Hi, I want to execute some commands via ssh. errupdate <enter> =5A7598C3: <enter> Report=False <enter> Log=False <enter> <CTRL + D> <CTRL + D> how can i do this ? I suppose i have to use file descriptor but i don't know how ? Thank you (2 Replies)
Discussion started by: khalidou13
2 Replies

9. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

10. Shell Programming and Scripting

Linux find command seems to not transmit all the result to the '-exec command'

Hello. From a script, a command for a test is use : find /home/user_install -maxdepth 1 -type f -newer /tmp/000_skel_file_deb ! -newer /tmp/000_skel_file_end -name '.bashrc' -o -name '.profile' -o -name '.gtkrc-2.0' -o -name '.i18n' -o -name '.inputrc' Tha command... (3 Replies)
Discussion started by: jcdole
3 Replies
expr(1) 							   User Commands							   expr(1)

NAME
expr - evaluate arguments as an expression SYNOPSIS
/usr/bin/expr argument... /usr/xpg4/bin/expr argument... /usr/xpg6/bin/expr argument... DESCRIPTION
/usr/bin/expr, /usr/xpg4/bin/expr The expr utility evaluates the expression and writes the result to standard output. The character 0 is written to indicate a zero value and nothing is written to indicate a null string. /usr/xpg6/bin/expr The expr utility evaluates the expression and writes the result to standard output followed by a NEWLINE. If there is no result from expr processing, a NEWLINE is written to standard output. OPERANDS
The argument operand is evaluated as an expression. Terms of the expression must be separated by blanks. Characters special to the shell must be escaped (see sh(1)). Strings containing blanks or other special characters should be quoted. The length of the expression is lim- ited to LINE_MAX (2048 characters). The operators and keywords are listed below. The list is in order of increasing precedence, with equal precedence operators grouped within {} symbols. All of the operators are left-associative. expr | expr Returns the evaluation of the first expr if it is neither NULL nor 0; otherwise, returns the evaluation of the second expr if it is not NULL; otherwise, 0. expr & expr Returns the first expr if neither expr is NULL or 0, otherwise returns 0. expr{ =, >, >=, <, <=, !=} expr Returns the result of an integer comparison if both arguments are integers, otherwise returns the result of a string comparison using the locale-specific coalition sequence. The result of each comparison will be 1 if the specified relationship is TRUE, 0 if the rela- tionship is FALSE. expr { +, - } expr Addition or subtraction of integer-valued arguments. expr { *, /, %} expr Multiplication, division, or remainder of the integer-valued arguments. expr : expr The matching operator : (colon) compares the first argument with the second argument, which must be an internationalized basic regular expression (BRE), except that all patterns are anchored to the beginning of the string. That is, only sequences starting at the first character of a string are matched by the regular expression. See regex(5) and NOTES. Normally, the /usr/bin/expr matching operator returns the number of bytes matched and the /usr/xpg4/bin/expr matching operator returns the number of characters matched (0 on fail- ure). If the second argument contains at least one BRE sub-expression [(...)], the matching operator returns the string corresponding to 1. integer An argument consisting only of an (optional) unary minus followed by digits. string A string argument that cannot be identified as an integer argument or as one of the expression operator symbols. Compatibility Operators (x86 only) The following operators are included for compatibility with INTERACTIVE UNIX System only and are not intended to be used by non- INTERAC- TIVE UNIX System scripts: index string character-list Report the first position in which any one of the bytes in character-list matches a byte in string. length string Return the length (that is, the number of bytes) of string. substr string integer-1 integer-2 Extract the substring of string starting at position integer-1 and of length integer-2 bytes. If integer-1 has a value greater than the number of bytes in string, expr returns a null string. If you try to extract more bytes than there are in string, expr returns all the remaining bytes from string. Results are unspecified if either integer-1 or integer-2 is a negative value. EXAMPLES
Example 1 Adding an integer to a shell variable Add 1 to the shell variable a: example$ a=`expr $a + 1` Example 2 Returning a path name segment The following example emulates basename(1), returning the last segment of the path name $a. For $a equal to either /usr/abc/file or just file, the example returns file. (Watch out for / alone as an argument: expr takes it as the division operator. See NOTES below.) example$ expr $a : '.*/(.*)' | $a Example 3 Using // characters to simplify the expression Here is a better version of the previous example. The addition of the // characters eliminates any ambiguity about the division operator and simplifies the whole expression. example$ expr //$a : '.*/(.*)' /usr/bin/expr Example 4 Returning the number of bytes in a variable example$ expr "$VAR" : '.*' /usr/xpg4/bin/expr Example 5 Returning the number of characters in a variable example$ expr "$VAR" : '.*' ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of expr: LANG, LC_ALL, LC_COLLATE, LC_CTYPE, LC_MESSAGES, and NLSPATH. EXIT STATUS
As a side effect of expression evaluation, expr returns the following exit values: 0 If the expression is neither NULL nor 0. 1 If the expression is either NULL or 0. 2 For invalid expressions. >2 An error occurred. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ |CSI |enabled | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ SEE ALSO
basename(1), ed(1), sh(1), Intro(3), attributes(5), environ(5), regex(5), standards(5) DIAGNOSTICS
syntax error Operator and operand errors. non-numeric argument Arithmetic is attempted on such a string. NOTES
After argument processing by the shell, expr cannot tell the difference between an operator and an operand except by the value. If $a is an =, the command: example$ expr $a = '=' looks like: example$ expr = = = as the arguments are passed to expr (and they are all taken as the = operator). The following works: example$ expr X$a = X= Regular Expressions Unlike some previous versions, expr uses Internationalized Basic Regular Expressions for all system-provided locales. Internationalized Regular Expressions are explained on the regex(5) manual page. SunOS 5.11 29 Aug 2003 expr(1)
All times are GMT -4. The time now is 10:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy