How to check the extension of a filename in a bash script?
I think you want to say "Are the last four characters of $file equal to .txt
?" If so, you can use the following:
if [ "${file: -4}" == ".txt" ]
Note that the space between file:
and -4
is required, as the ':-' modifier means something different.
Error - There is no script engine for file extension .vbs when using Git Bash Here in Windows 7
The problem is caused by associating .vbs
files with a program other than Microsoft Windows Based Script Host
(the default). In my case, I had associated the files with Notepad++. I was able to solve it by running Notepad++ as an administrator and removing the file association for .vbs
files.
If you're not sure which program is causing the problem, you can find out by searching for "Change the file type associated with a file extension" from the start menu. To fix the problem from there, make sure that .vbs files have Microsoft Windows Based Script Host
set as the current default program.
How can I get file extensions with JavaScript?
Newer Edit: Lots of things have changed since this question was initially posted - there's a lot of really good information in wallacer's revised answer as well as VisioN's excellent breakdown
Edit: Just because this is the accepted answer; wallacer's answer is indeed much better:
return filename.split('.').pop();
My old answer:
return /[^.]+$/.exec(filename);
Should do it.
Edit: In response to PhiLho's comment, use something like:
return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined;
Required file extension for JavaScript files
No, they could also be .whatever
and they would work anyways.
The script:
<script type="text/javascript" src="file.txt"></script>
Tells the html to use that file as a JS, no matter the extention.
I used to write my JS with php and so they could contain PHP code, but you can get in trouble sometimes if you do that.
You should also look at the Search Engines optimisation; search engines are expecting .js
files to be .js
and they can go askew because of wrong extensions and your site may lose some points and positions.
An important point is the server, as it "knows" the file contents is javascript if they are .js
extension and so it treats it in the right manner, and does what's expected.
The best practice is to use proper file extensions.
(But this does not mean you can't use others).
Related Topics
How to Run Multiple Programs in a Sequence
Safely Remembering Ssh Credentials in Bash Script
Find the Average of Fields in the Columns
A Way to Prevent Bash from Parsing Command Line W/Out Using Escape Symbols
How to Mail Script Output in Table Format
Calling Printf from Assembly Language on 64Bit and 32Bit Architecture Using Nasm
When Installing Rust Toolchain in Docker, Bash 'Source' Command Doesn't Work
How to Escape the Bang (!) Character in Linux Bash Shell
Why Does This Shell Pipeline Exit
How to Start/Restart/Stop Apache Server on Linux as Non-Root User
Use Bash Variable Within Slurm Sbatch Script
Run Shell Command and Don't Wait for Return
Shell Script to Delete Files When Disk Is Full
How to View Thread Id of a Process Which Has Opened a Socket Connection
Setting Environment Variable to a Large Value -> "Argument List Too Long"
Opening New Gnome-Terminal (V3.28+) with Multiple Tabs and Different Commands