微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cat仅打印一个文件描述符

如何解决cat仅打印一个文件描述符

我试图了解文件描述符的工作原理。

下面的script2.sh为什么不输出b.txt的内容

$ cat a.txt
Hello
$ cat b.txt
World

$ cat script1.sh 
cat a.txt b.txt
$ ./script1.sh 
Hello
World

$ cat script2.sh 
exec 19<a.txt
exec 20<b.txt
cat <&19 <&20
$ ./script2.sh 
World

解决方法

#include<iomanip> #include<cmath> using namespace std; int main() { int i,j,k,n,N; cout.precision(4); //set precision cout.setf(ios::fixed); cout << "\nEnter the no. of data pairs to be entered:\n"; //To find the size of arrays that will store x,y,and z values cin >> N; double x[N],y[N]; cout << "\nEnter the x-axis values:\n"; //Input x-values for (i = 0; i < N; i++) cin >> x[i]; cout << "\nEnter the y-axis values:\n"; //Input y-values for (i = 0; i < N; i++) cin >> y[i]; cout << "\nWhat degree of Polynomial do you want to use for the fit?\n"; cin >> n; // n is the degree of Polynomial double X[2 * n + 1]; //Array that will store the values of sigma(xi),sigma(xi^2),sigma(xi^3)....sigma(xi^2n) for (i = 0; i < 2 * n + 1; i++) { X[i] = 0; for (j = 0; j < N; j++) X[i] = X[i] + pow(x[j],i); //consecutive positions of the array will store N,sigma(xi),sigma(xi^3)....sigma(xi^2n) } double B[n + 1][n + 2],a[n + 1]; //B is the Normal matrix(augmented) that will store the equations,'a' is for value of the final coefficients for (i = 0; i <= n; i++) for (j = 0; j <= n; j++) B[i][j] = X[i + j]; //Build the Normal matrix by storing the corresponding coefficients at the right positions except the last column of the matrix double Y[n + 1]; //Array to store the values of sigma(yi),sigma(xi*yi),sigma(xi^2*yi)...sigma(xi^n*yi) for (i = 0; i < n + 1; i++) { Y[i] = 0; for (j = 0; j < N; j++) Y[i] = Y[i] + pow(x[j],i) * y[j]; //consecutive positions will store sigma(yi),sigma(xi^2*yi)...sigma(xi^n*yi) } for (i = 0; i <= n; i++) B[i][n + 1] = Y[i]; //load the values of Y as the last column of B(Normal Matrix but augmented) n = n + 1; //n is made n+1 because the Gaussian Elimination part below was for n equations,but here n is the degree of polynomial and for n degree we get n+1 equations cout << "\nThe Normal(Augmented Matrix) is as follows:\n"; for (i = 0; i < n; i++) //print the Normal-augmented matrix { for (j = 0; j <= n; j++) cout << B[i][j] << setw(16); cout << "\n"; } for (i = 0; i < n; i++) //From now Gaussian Elimination starts(can be ignored) to solve the set of linear equations (Pivotisation) for (k = i + 1; k < n; k++) if (B[i][i] < B[k][i]) for (j = 0; j <= n; j++) { double temp = B[i][j]; B[i][j] = B[k][j]; B[k][j] = temp; } for (i = 0; i < n - 1; i++) //loop to perform the gauss elimination for (k = i + 1; k < n; k++) { double t = B[k][i] / B[i][i]; for (j = 0; j <= n; j++) B[k][j] = B[k][j] - t * B[i][j]; //make the elements below the pivot elements equal to zero or elimnate the variables } for (i = n - 1; i >= 0; i--) //back-substitution { //x is an array whose values correspond to the values of x,z.. a[i] = B[i][n]; //make the variable to be calculated equal to the rhs of the last equation for (j = 0; j < n; j++) if (j != i) //then subtract all the lhs values except the coefficient of the variable whose value is being calculated a[i] = a[i] - B[i][j] * a[j]; a[i] = a[i] / B[i][i]; //now finally divide the rhs by the coefficient of the variable to be calculated } cout << "\nThe values of the coefficients are as follows:\n"; for (i = 0; i < n; i++) cout << "x^" << i << "=" << a[i] << endl; // Print the values of x^0,x^1,x^2,x^3,.... cout << "\nHence the fitted Polynomial is given by:\ny="; for (i = 0; i < n; i++) cout << " + (" << a[i] << ")" << "x^" << i; cout << "\n"; return 0; } 表示“用FD 19替换标准输入”,<&19表示“用FD 20替换标准输入”。这些互相伤害。如果您要读取两个FD,请改为使用<&20

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。