Perl Script in batch - regex

I got a shell script an need to "convert" it to a batch file!
SET LOADERMID=app\run
##### SHELL PART #####
cd /d %BASEDIR%
LOADERMID=${LOADERMID//\//\\\/}
# Copy & minify index.html to dist
cat "$SRCDIR/index.html" | tr '\n' ' ' | \
perl -pe "
s/<\!--.*?-->//g; # Strip comments
s/isDebug: *1/deps:['$LOADERMID']/; # Remove isDebug, add deps
s/<script src=\"$LOADERMID.*?\/script>//; # Remove script app/run
s/\s+/ /g; # Collapse white-space" > "$DISTDIR/index.html"
echo "Build complete"
######## SHELL PART END ##########
I got the most of it working expect the following part that copies a file to another directory and does some regex/replacing stuff.
Now I donĀ“t know how to do it in a batch file.
Is it possible to call perl in batch?
Is it possible to do that perl code in a batch file?
Thanks for help!

If you find yourself needing to run bash scripts in a windows environment, you could also try MinGW and MSYS. MSYS, in particular, gives you a nice unixy command line environment that is useful for doing real work.

Related

Shell Script - Get name of dynamically generated file

I'm very new to shell script and therefore I don't now very much about it.
I have an application, which creates a java file with a half unknown name, and now I try to write a script, which needs this name.
The known name of the file is /target/plugin-<dyn>.jar, the <dyn> part is unknown and could be nearly anything (btw it is mostly a version number with variable text parts).
Now I want to save plugin-<dyn> (without the .jar) in a variable for later use. I looked very much in the internet, but I can't find a solution.
If you need get file name without extension .jar. You can refer my bash script below:
# for loop all files in target directory that matched plugin-*.jar
for f in target/plugin-*.jar
do
# print file name without extension .jar
echo ${f%.*}
done
UPDATED:
# for loop all files in target directory that matched plugin-*.jar
for f in target/plugin-*.jar
do
# print file name without extension .jar
filename="${f##*/}" # get plugin-*.jar
echo ${filename%.*} # print plugin-* without jar
done
try this
filename="/target/plugin-<dyn>.jar"
echo ${filename} | awk -F [/.] '{print $(NF - 1)}'
echo ${filename} | sed 's/.*\/\([^/]*\)\.jar/\1/'
but if <dyn> has a slash, comma or point. it may not work

Find and repalce string that includes quotes within files in multiple directories - unix aix

So here's the scenario. I'd like to change the following value from true to false in 100's of files in an installation but can't figure out the command and been working on this for a few days now. what i have is a simple script which looks for all instances of a file and stores the results in a file. I'm using this command to find the files I need to modify:
find /directory -type f \ ( -name 'filename' \) > file_instances.txt
Now what i'd like to do is run the following command, or a variation of it, to modify the following value:
sed 's/directoryBrowsingEnabled="false"/directoryBrowsingEnabled="true"/g' $i > $i
When i tested the above command, it had blanked out the file when it attempted to replace the string but if i run the command against a single file, the change is made correctly.
Can someone please shed some light on to this?
Thank you in advance
What has semi-worked for me is the following:
You can call sed with the -i option instead of doing > $i. You can even do a backup of the old file just in case you have a problem by adding a suffix.
sed -e 'command' -i.backup myfile.txt
This will execute command inplace on myfile.txt and save the old file in myfile.txt.backup.
EDIT:
Not using -i may indeed result in blank files, this is because unix doesn't like you to read and write at the same time (it leads to a race condition).
You can convince yourself of this by some simple cat commands:
$ echo "This is a test" > test.txt
$ cat test.txt > test.txt # This will return an error cat being smart
$ cat <test.txt >test.txt # This will blank the file, cat being not that smart
On AIX you might be missing the -i option of sed. Sad. You could make a script that moves each file to a tmp file and redirects (with sed) to the original file or try using a here-construction with vi:
cat file_instances.txt | while read file; do
vi ${file}<<END >/dev/null 2>&1
:1,$ s/directoryBrowsingEnabled="false"/directoryBrowsingEnabled="true"/g
:wq
END
done

Bash script to change file extension using regex

I have a lot of files i've copied over from my iphone file system, to start with they were mp3 files, but app on iphone changed their names to some random staff which looks like:
1c03e04cc1bbfcb0c1237f57f1d0ae2e.mp3?extra=f7NhT68pNkmEbGA_I1WbVShXQ2E2gJAGBKSEyh3hf0hsbLB1cqnXDuepYA5ubcFm_B3KSsrXDuKVtWVAUh_MAPeFiEHXVdg
I only need to remove part of file name after mp3. Please give me a script - there are more than 600 files, and manually it is impossible.
you can use rename command:
rename "s/mp3\?.*/mp3/" *.mp3*
#!/bin/bash
shopt -s nullglob
for F in *.mp3\?*; do
echo mv -v -- "$F" "${F%%.mp3\?*}.mp3"
done
Save it to a script like script.sh then run as bash /path/to/script.sh in the directory where the files exist.
Remove echo when you find it correct already.

running an executable in its own working directory

I added a button to my gvim toolbar which runs a .sh file. The .sh file runs scons to build my c++ application in the /build subdirectory and runs it. The problem is that when the application is running, its current working directory is the folder that contains the .sh file (not the applications /build subdirectory)! So how do I run a built c++ applications executable (linux) from a .sh file, so that its working directory would be the folder which contains executable?
Just
cd $(dirname "$0")
./exec_test
Note, you need ./exec_test, not exec_test unless the directory is actually already in PATH
Here's an example of something similar (I don't use scons.)
I add my toolbar icon with:
:amenu ToolBar.mytool :!/home/me/code/misc/foo.sh "%"
For me, when I click this, vim runs the script in the same working directory as vim.
foo.sh contains:
#!/bin/bash
set -e
# You should see the name of your file.
# It might just be "my_file.c"
echo "$1"
# This will tell you where your script is current cd'd to.
pwd
# `cd` to where the file passed on the command line is:
cd "$(dirname "$1")"
# Look for "CMakeLists.txt"
# You only need this loop if your build file / program might be up a few directories.
# My stuff tends to be:
# / - project root
# CMakeLists.txt
# src/
# foo.c
# bar.c
while true; do
# We found it.
if [[ -e "CMakeLists.txt" ]]; then
break
fi
# We didn't find it. If we're at the root, just abort.
if [[ "`pwd -P`" = "/" ]]; then
echo "Couldn't find CMakeLists.txt." >&2
exit 1
fi
cd ..
done
# I do builds in a separate directory.
cd build && make
You'd replace CMakeLists.txt with SConstruct, and the last cd build && make with scons, or something appropriate to scons.

Copy and Rename Multiple Files with Regular Expressions in bash

I've got a file structure that looks like:
A/
2098765.1ext
2098765.2ext
2098765.3ext
2098765.4ext
12345.1ext
12345.2ext
12345.3ext
12345.4ext
B/
2056789.1ext
2056789.2ext
2056789.3ext
2056789.4ext
54321.1ext
54321.2ext
54321.3ext
54321.4ext
I need to rename all the files that begin with 20 to start with 10; i.e., I need to rename B/2022222.1ext to B/1022222.1ext
I've seen many of the other questions regarding renaming multiple files, but couldn't seem to make it work for my case. Just to see if I can figure out what I'm doing before I actually try to do the copy/renaming I've done:
for file in "*/20?????.*"; do
echo "{$file/20/10}";
done
but all I get is
{*/20?????.*/20/10}
Can someone show me how to do this?
You just have a little bit of incorrect syntax is all:
for file in */20?????.*; do mv $file ${file/20/10}; done
Remove quotes from the argument to in. Otherwise, the filename expansion does not occur.
The $ in the substitution should go before the bracket
Here is a solution which use the find command:
find . -name '20*' | while read oldname; do echo mv "$oldname" "${oldname/20/10}"; done
This command does not actually do your bidding, it only prints out what should be done. Review the output and if you are happy, remove the echo command and run it for real.
Just wanna add to Explosion Pill's answer.
On OS X though, you must say
mv "${file}" "${file_expression}"
Or the mv command does not recognize it.
Brace expansions like :
{*/20?????.*/20/10}
can't be surrounded by quotes.
Instead, try doing (with Perl rename) :
rename 's/^10/^20/' */*.ext
You can do this using the Perl tool rename from the shell prompt. (There are other tools with the same name which may or may not be able to do this, so be careful.)
If you want to do a dry run to make sure you don't clobber any files, add the -n switch to the command.
note
If you run the following command (linux)
$ file $(readlink -f $(type -p rename))
and you have a result like
.../rename: Perl script, ASCII text executable
then this seems to be the right tool =)
This seems to be the default rename command on Ubuntu.
To make it the default on Debian and derivative like Ubuntu :
sudo update-alternatives --set rename /path/to/rename
The glob behavior of * is suppressed in double quotes. Try:
for file in */20?????.*; do
echo "${file/20/10}";
done