سؤال

For example, I have

./run.sh < file.dat

How do I get the argument "file.dat" in run.sh?

هل كانت مفيدة؟

المحلول

On Linux, you can get this data from /proc:

#!/bin/bash
readlink /proc/$$/fd/0

This will print the path of whatever's opened as stdin, such as file.dat in your example.

Note that getting the filename this way is not the correct way of working with the file, and should be used purely for debugging and informational purposes.

To work with the data you should instead simply read from stdin. For example, read lines with IFS= read -r myline; echo "$myline" to read a single line, or mydata=$(cat); echo "$mydata" to read all of it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top