Mihic
Mihic

Reputation: 401

How to get a filename in a bash script without the keyboard

I want to write a script for linux, that will first copy a movie/series file to cache with something like:

cat /filepath/filename > /dev/null 

and than open the same file in vlc.

The problem is getting the file name and path in to the script. I would like to simply double click a file, or somehow make this a faster process than typing this manually (especially because the file names of some series are just inconsistent and hard to type, even with auto-complete).

This is useful for watching movies or series on a laptop/netbook, since it allows the disk to spin down.

Upvotes: 1

Views: 335

Answers (1)

Cefn Hoile
Cefn Hoile

Reputation: 441

You should be able to create your own 'program' in a bash script which takes its first argument to be the filename using the convention "$1".

The bash script should look something like the below. I tested it, storing the script in the file cachedvlc.sh. The inverted commas helping to handle whitespace and weird characters...

#!/bin/bash
cat "$1" > /dev/null
vlc "$1"

...and will need to be made executable by changing its permissions through the file manager or running this in the terminal...

chmod u+x cachedvlc.sh

Then within your operating system, associate your bash script with the type of file you want to launch. For example on Ubuntu, you could add your script and call it 'Cached VLC' to the Menu using the 'Main Menu' application, then right-click on the file in Nautilus and choose 'Open with' to select your bash script.

After this, double-clicking or right-clicking on a file within your filemanager should be good enough to launch a cached view. This assumes what you say about caching is in fact correct, which I can't easily check.

Upvotes: 2

Related Questions