Find & replace a character in filenames from terminal - macos

I am trying to use ren-regexp to replace a character in filenames like so:
./ren-regexp.pl "s/_/-/g" *.jpg
Which I think should replace _ with - in filenames that are jpgs, but what I get is:
./ren-regexp.pl: line 4: syntax error near unexpected token `newline'
./ren-regexp.pl: line 4: `<!DOCTYPE html>'
I also tried
perl ren-regexp.pl "s/_/-/g" *.jpg
Which resulted in lots of errors such as:
Bareword found where operator expected at ren-regexp.pl line 252, near "time class"
(Do you need to predeclare time?)
Bareword found where operator expected at ren-regexp.pl line 252, near ""js-relative-date" datetime"
(Missing operator before datetime?)
Bareword found where operator expected at ren-regexp.pl line 252, near ""2011-04-13T16:40:41-07:00" title"
(Missing operator before title?)
Number found where operator expected at ren-regexp.pl line 252, near "April 13"
(Do you need to predeclare April?)
Bareword found where operator expected at ren-regexp.pl line 262, near ""/msabramo/ren-regexp/tree/17026c762c41e2b88ed91bf78b63e54859b706e5" class"
(Missing operator before class?)
I tried using the examples shown on the GitHub page as well as here:
Mass replace characters in filenames from terminal?
Where am I going wrong? Running just 'perl ren-regexp.pl' by itself also results in the above set of errors.

It looks like you downloaded the script from
https://github.com/msabramo/ren-regexp/blob/master/ren-regexp.pl
instead of
https://raw.github.com/msabramo/ren-regexp/master/ren-regexp.pl
The former link will actually give you an HTML document that is unsuitable for passing into perl.
Or you could view the first link in a browser, and then copy and paste the Perl code to a file (and strip the line numbers).

Related

How do You Get Vim to Syntax Highlight Arithmetic Operators for Python?

I found that, while in vim, if I were to give the following commands
:syn match pythonOperator "="
or
:syn match pythonOperator "+"
etc.
Vim would highlight =, and +; however, when I add this to ~/.config/nvim/after/syntax/python.vim, it doesn't work. What is the proper way to extend what Vim highlights?
EDIT:
This is the output of the command :filter /python/ scriptnames:
61: /usr/share/nvim/runtime/ftplugin/python.vim
62: /usr/share/nvim/runtime/autoload/provider/python3.vim
63: /usr/share/nvim/runtime/autoload/provider/pythonx.vim
65: /usr/share/nvim/runtime/autoload/provider/python.vim
66: /usr/share/nvim/runtime/indent/python.vim
67: /usr/share/nvim/runtime/syntax/python.vim
68: ~/.config/nvim/after/syntax/python.vim
As seen with 68:, my python syntax file is being loaded, but for some reason the commands that I put in are being ignored?
For clarification, I added litterally
syn match pythonOperator "+"
syn match pythonOperator "-"
to ~/.config/nvim/after/syntax/python.vim which was originally copied from /usr/share/nvim/runtime/syntax/python.vim. If it matters where I added it, I added it right after line 104 in the file which is after the block of keyword definitions.

While using a single quote in a string in shell with Ruby I get sh: -c: line 0: unexpected EOF while looking for matching `''

This works fine in Ruby, and it should work:
puts 'don\'t'
But I want to run the same in BASH with Ruby:
%x(echo 'don\'t')
I get this error:
sh: -c: line 0: unexpected EOF while looking for matching''`
Same error occurs with ``, system(), Open3
My actual code snippet:
require 'open3'
module XdoTool
BIN = 'xdotool'
EXEC = ::ENV['PATH'].split(::File::PATH_SEPARATOR).map { |path| ::File.join(path, BIN) if ::File.executable?(::File.join(path, BIN)) }.compact.last
raise RuntimeError, "No #{BIN} found in the exported paths. Please make sure you have #{BIN} installed" unless EXEC
class << self
def type(str)
Open3.capture2("#{EXEC} type --delay 0 '#{str = str.gsub("'", '\'')}'")
str
end
end
end
# Types the quoted text anywhere.
XdoTool.type("What is the reason of the error?")
# sh: -c: line 0: unexpected EOF while looking for matching `''
# sh: -c: line 1: syntax error: unexpected end of file
XdoTool.type("What's the reason of the error?")
Please do note that the str can have anything. It can contain alphanumeric characters, symbols, emojis, or combination of all these things. How can I get around the problem with quotes here?
In shell, you simply cannot include a single quote inside a single-quoted string. It has to be in a double-quoted string. That means, if you want an argument that contains both, you need to concatenate separately quoted stings together.
echo 'He said "I can'"'"t'"'
or escape the double quotes inside a double-quoted string
echo "He said \"I can't\""
(Some shells provide yet another form of quoting that can contain an escaped single quote, namely $'He said "I can\'t"'. However, that's an extension to the POSIX standard that you can't assume is supported by the
shell Ruby will use to execute your command.)

Why does (*.foo|bar) cause an error, but !(*.foo|bar) does not?

Error
line 852: syntax error near unexpected token `('
Line 852 is the last line of a function:
touch --reference="$KERNEL_FILE" "$moduledest"/modules.(*.bin|devname|softdep)
No error
When inserting the exclamation mark ! before (:
touch --reference="$KERNEL_FILE" "$moduledest"/modules.!(*.bin|devname|softdep)
How to touch the inverse of !(*.bin|devname|softdep)?
/modules.alias.bin
/modules.builtin.bin
/modules.dep.bin
/modules.devname
/modules.softdep
/modules.symbols.bin
Extended glob
The pattern you are trying to negate match with the exclamation mark is an "extended glob". Enabled somewhere in the script with a command alike shopt -s extglob.
The negation form with extended globbing syntax is defined as !(list):
!(list) Matches anything but the given patterns.
The inverse of that negative match in this case is the syntaxis #(list):
#(list) Matches one of the given patterns

added a line to an existing shell script and

now I get a bizarre error as if I have changed/lost a closing argument such as fi, statement closure such as , or a hidden series of tabs.
This is the message I get in my err file:
./cron_run.sh: line 156: syntax error near unexpected token `else'
./cron_run.sh: line 156: ` else'
Again, I did not touch these lines. Not even close and what I added was another operation to dump a mongo collection to the backup directory. So I had:
...start of script
mongodump... # this was existing
mongodump... # this was the addition
...rest of script (about 70) lines unchanged
Key point:
the above lines worked
the code/server executed as expected
the error/crash occurred at the end of the process after execution (so the whole thing actually worked!)
I looked at the code with syntax highlighting (vim, nano) and I cannot see anything wrong with it (at least not an obvious thing such as a bracket, fi or missing back tick)!
Any suggestions?

Call a function from an external script

I have 2 scripts:
/home/bin/test.sh
#!/bin/bash
. /home/bin/test_functions.sh
test
/home/bin/test_functions.sh
#!/bin/sh
test()
{
echo "this is a test"
}
I wanted to call the function from the external script and execute it in the main script. Yet I've been receiving these errors:
'home/bin/test_functions.sh: line 2: syntax error near unexpected token `
'home/bin/test_functions.sh: line 2: `test()
What could be wrong with what I'm doing?
It appears that test_functions.sh is in DOS format and bash is choking on the \r\n line endings. Use dos2unix to convert it to UNIX line endings.
You can tell because what bash is trying to output is this:
/home/bin/test_functions.sh: line 2: syntax error near unexpected token `\r'
/home/bin/test_functions.sh: line 2: `test()\r'
But the carriage returns \r cause the single quotes to end up at the beginning of the error messages, overwriting the leading /.

Resources