So, I need to write a shell script that accepts a file path as an argument and uses it within the shell script. I know that I could do something like:
$ ./script filename
With this solution, this would make it so that I could access this filename string using $1
in the shell script.
However, I'm wondering if it is possible to use input redirection instead so I could pass the filename using it:
$ ./script < filename
If so, how would I access this filename in the shell script?
I'm fairly new to shell script coding so I'm not sure if this is even possible.
You can extract the name of file passed with <
by listing /proc/<pid>/fd
directory, as follows:
ls -ltr /proc/$$/fd
Then
$ cat myscript.sh
ls -ltr /proc/$$/fd
$ ./myscript.sh < hello
total 0
lr-x------ 1 u g 64 Feb 25 08:42 255 -> /tmp/myscript.sh
lrwx------ 1 u g 64 Feb 25 08:42 2 -> /dev/pts/0
lrwx------ 1 u g 64 Feb 25 08:42 1 -> /dev/pts/0
lr-x------ 1 u g 64 Feb 25 08:42 0 -> /tmp/hello
I can't guess whether this is useful
And doesn't work when input is passed through a pipe
$ cat hello| ./myscript.sh
total 0
lr-x------ 1 u p 64 Feb 25 08:50 255 -> /tmp/myscript.sh
lrwx------ 1 u p 64 Feb 25 08:50 2 -> /dev/pts/0
lrwx------ 1 u p 64 Feb 25 08:50 1 -> /dev/pts/0
lr-x------ 1 u p 64 Feb 25 08:50 0 -> pipe:[82796]
Alternatively, you can use lsof and a little line handling to extract value
filename=$(/usr/sbin/lsof -p $$| grep " 0u"| cut -c 60-)
You cannot access the filename
in the case of ./script < filename
but only its contents. The reason is that the shell opens filename
and set the corresponding file descriptor to be the standard input of the ./script
.
With your second solution you don't access the filename, but your script's standard input is coming from the file.
With the first solution printing the file would look like this:
cat "$1"
With the second:
while read line; do echo "$line"; done
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.