Using pdfgrep with spaces in variable name for seaching phrase into pdf file - windows-subsystem-for-linux

Currently Im using on Windows 10, subsystem for Linux Ubuntu 20.04 the command pdfgrep
for to use in my script. Lets explain a little bit in more detail.
I want to search in a pdf file in my folder for an articlenumber that contains spaces in it.
But in my little script I get an error : first you see an echo with the whole pdfgrep command with the search pattern (= articlenumber) and the pdf file located in my folder
/usr/bin/pdfgrep '4022 622 48402' ../testdir/'4022 622 48402 - B263486P010 DY 2022-8-22.pdf'
On the command line, above pdfgrep command seems to work fine.
Below is the part of my script :
#!/usr/bin/bash
#
#
#
clear
echo
inputfile="`ls ../testdir/*.pdf`"
inputfile2="${inputfile:11:46}"
inputfile3=\'$inputfile2\'
doctp1=${inputfile: -3}
articlenumber1=${inputfile:11:14}
articlenumber2=\'$articlenumber1\'
echo
read -p "Running pdfgrep with articlenumber. Push a key to continue ! " x
clear
echo
#case ${doctp1} in
# pdf)
clear
echo "/usr/bin/pdfgrep ${articlenumber2} ../testdir/${inputfile3}"
/usr/bin/pdfgrep ${articlenumber2} ../testdir/${inputfile3}
if [ $? -ne 0 ]
then
echo "No pdf files with given articlenumber present"
# echo "No pdf files with given articlenumber present" >> log_$articlenumber1_$(date +"%Y%m%d_%H%M").txt
fi
echo
read -p "Push a key to continue !" x
When I run the script I got this error :
pdfgrep: Could not open 622
pdfgrep: Could not open 48402'
pdfgrep: Could not open ../testdir/'4022
pdfgrep: Could not open 622
pdfgrep: Could not open 48402
pdfgrep: Could not open -
pdfgrep: Could not open B263486P010
pdfgrep: Could not open DY
pdfgrep: Could not open 2022-8-22.pdf'
Can you help me with finding a solution for this ? Is there anything that I do wrong here ?
Please let me know. All comments are welcome !

Related

Is it possible to use the "code" command in SSH'ed terminal to open VS Code on local machine with SSH extension?

Something I love about VS Code is that when I am using a terminal in WSL, I can run code file.txt, and it will open that file with VS Code on my local using the WSL remote extension.
Is it possible to do a similar thing with SSH? I.e., if I am SSH'ed into a remote host, is it possible to set things up so that running code file.txt will open VS Code on my local machine, connected via the remote SSH extension to open that file?
I found much better & simple answer thanks to this post.
Simply create new script file named code with below contents & put file under any folder from $PATH. (echo $PATH to see what folders you can use)
#! /usr/bin/env zsh
local max_retry=10
for i in {1..$max_retry}
do
local script=$(echo ~/.vscode-server/bin/*/bin/remote-cli/code(*oc[$i]N))
if [[ -z ${script} ]]
then
echo "VSCode remote script not found"
exit 1
fi
local socket=$(echo /run/user/$UID/vscode-ipc-*.sock(=oc[$i]N))
if [[ -z ${socket} ]]
then
echo "VSCode IPC socket not found"
exit 1
fi
export VSCODE_IPC_HOOK_CLI=${socket}
${script} $# > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
exit 0
fi
done
echo "Failed to find valid VS Code window"
Bash version
#! /bin/bash
max_retry=10
for i in $(seq 1 $max_retry)
do
recent_folder=$(ls ~/.vscode-server/bin/ -t | head -n$i)
script=$(echo ~/.vscode-server/bin/$recent_folder/bin/remote-cli/code)
if [[ -z ${script} ]]
then
echo "VSCode remote script not found"
exit 1
fi
socket=$(ls /run/user/$UID/vscode-ipc-* -t | head -n$i)
if [[ -z ${socket} ]]
then
echo "VSCode IPC socket not found"
exit 1
fi
export VSCODE_IPC_HOOK_CLI=${socket}
${script} $#
if [ "$?" -eq "0" ]; then
exit 0
fi
done
echo "Failed to find valid VS Code window"
Update
Above script doesn't work with recent updates. I had to change first line to
local script=$(echo ~/.vscode-server/bin/*/bin/remote-cli/code(*oc[1]N))
Update2
Original script may fail if recently opened ssh window is closed, yet there is another SSHed window open. I have enhanced the script to enable retrying the command with recent N(default 10) windows.
You shouldn't have to do anything. VSCode automatically sets the path/PATH to the code in the path/PATH environment variable depending on your shell. See this response. You might be overwriting your path/PATH like I was. I was accidentally overwriting path in ~/.cshrc and PATH in ~/.bashrc and was running into the same issue. After fixing it, I can run code on the command line. which code returns the location of the command.
Until I spent time to figure it out, I was using the two methods mentioned below. Both of which worked for me in bash; you can modify it for your shell as you see fit. But really fix your path/PATH rather than using these methods.
Adding location of code to the PATH in ~/.bashrc
export PATH=${VSCODE_GIT_ASKPASS_NODE%/*}/bin:$PATH
OR
Setting alias to code in ~/.bashrc
alias code="${VSCODE_GIT_ASKPASS_NODE%/*}/bin/code"
More on path vs. PATH here and here
Yes, sort of.
From a VSCode terminal run the command
env | grep VSCODE_IPC_HOOK_CLI
then copy-and-paste that line that line with export into your ssh terminal.
After that, you should be able to run code from your ~/.vscode-server/bin/XXX/bin directory.
VSCode terminal
SSH terminal
Update:
You can to automate this with a .bashrc and .profile to place the IPC code into a temp file, and source that when you do your ssh login.
For example, this works for me...
Append this to ~/.bashrc
#
if [ "$VSCODE_IPC_HOOK_CLI" != "" ]; then
cat >$HOME/.vscode_env.sh <<EOF
#
if [ "\$VSCODE_IPC_HOOK_CLI" = "" ]; then
export VSCODE_IPC_HOOK_CLI="$VSCODE_IPC_HOOK_CLI"
alias code="${VSCODE_GIT_ASKPASS_NODE%/*}/bin/code"
fi
EOF
fi
And append this to your ~/.profile
[ -f $HOME/.vscode_env.sh ] && . $HOME/.vscode_env.sh
(There may be more elegant ways. And you still have to start at least 1 terminal in your remote VSCode session.)
this works to me
if [[ -n "$SSH_CLIENT" || -n "$SSH_TTY" ]]; then
local script=$(echo ~/.vscode-server/bin/*/bin/remote-cli/code(*oc[1]N))
if [[ -z ${script} ]]
then
echo "VSCode remote script not found"
exit 1
fi
local socket=$(echo /run/user/$UID/vscode-ipc-*.sock(=oc[1]N))
if [[ -z ${socket} ]]
then
echo "VSCode IPC socket not found"
exit 1
fi
export VSCODE_IPC_HOOK_CLI=${socket}
alias code=${script}
fi
Use the below commands to open a folder or a file on the remote terminal.
Note: vscode-server must be already installed on the remote host (It would be, if you have already connected to it). Also the absolute path has to be specified for the file or folder. Use -n to launch in new window,-r to reuse same window.
code --folder-uri <absolute-path>
code --file-uri <absolute-path-file-name>
Example:
code -r --folder-uri /home/myscripts/src
code -n --file-uri /home/myscripts/src/math/sample.py

iTerm2: quick download over SSH using CMD+click

iTerm2 allows you to click on a link (CMD+click) and open it quickly. However, when working over SSH, this doesn't work. Is it possible to enable this functionality, so that I can CMD+click a file, and it will automatically download into a folder on my local machine?
Thanks!
This is actually possible with Shell Integration installed. Note that Shell Integration will need to be installed on any server that you are ssh'ing into, not just on your local machine. From this link:
iTerm has recently introduced a feature called Shell Integration. Using this feature, we can upload and download files conveniently directly from iTerm 2. Drag a file into the window when pressing Option Key uploads the file to the remote ssh connection. Right-click on a file using ls command will bring up a context list containing downloading the file.
Click “iTerm2->Install Shell Integration” when sshing into the remote server.
Ensure the server has a correct FQDN as hostname and can be connected through this hostname. (You can use hostname -f to check it)
If you’re using private key authentication, then you should have id_rsa in your .ssh directory. However, you should also put id_rsa.pub in your .ssh directory to use this feature.
Sorry for the late answer, but I was just trying to do the same thing and came across your question. Thought I would post my findings once I found a solution.
I've not had much success with ⌘+Clicking to download via SCP in iTerm2 because I have a complex set of rules involving jump hosts in ~/.ssh/config.
But I have found an elegant work around: a shell function which writes to STDOUT to trigger iTerm2 into capturing the output and saving it as a file!
I keep the following snippet (Toolbelt → Snippets) which I execute to define a command download:
alias download="bash <(base64 -d <<<'IyEvYmluL2Jhc2gKaWYgWyAkIyAtbHQgMSBdOyB0aGVuCiAgZWNobyAiVXNhZ2U6ICQwIGZpbGUg
Li4uIgogIGV4aXQgMQpmaQpmb3IgZmlsZW5hbWUgaW4gIiRAIgpkbwogIGlmIFsgISAtciAiJGZp
bGVuYW1lIiBdIDsgdGhlbgogICAgZWNobyBGaWxlICRmaWxlbmFtZSBkb2VzIG5vdCBleGlzdCBv
ciBpcyBub3QgcmVhZGFibGUuCiAgICBjb250aW51ZQogIGZpCgogIGZpbGVuYW1lNjQ9JChlY2hv
IC1uICIkZmlsZW5hbWUiIHwgYmFzZTY0KQogIGZpbGVzaXplPSggJCh3YyAtYyAiJHtmaWxlbmFt
ZX0iKSApCiAgcHJpbnRmICJcMDMzXTEzMzc7RmlsZT1uYW1lPSR7ZmlsZW5hbWU2NH07c2l6ZT0k
e2ZpbGVzaXplWzBdfToiCiAgYmFzZTY0IDwgIiRmaWxlbmFtZSIKICBwcmludGYgJ1xhJwpkb25l
Cg==')"
The base64-encoded string decodes to:
#!/bin/bash
if [ $# -lt 1 ]; then
echo "Usage: $0 file ..."
exit 1
fi
for filename in "$#"
do
if [ ! -r "$filename" ] ; then
echo File $filename does not exist or is not readable.
continue
fi
filename64=$(echo -n "$filename" | base64)
filesize=( $(wc -c "${filename}") )
printf "\033]1337;File=name=${filename64};size=${filesize[0]}:"
base64 < "$filename"
printf '\a'
done
Which relies on iTerm2's download protocol
Sample session showing the notifications from iTerm2:

Move file, change permissions and rename it keeping the same extesion

Using zsh 5.2 on Fedora 24 workstation.
I want to be programatically able to:
move an image file (can have jpg/ jpeg/ png/ JPG/ PNG extensions)
from /tmp/folder1 to ~/Pictures
This file will have the same few initial characters --- prefix111.jpg OR prefix222.png, etc.
rename the file such that samefilename.JPG becomes 20161013.jpg
20161013 is today's date in yyyymmdd format
Note that the extension becomes small letters
And JPEG or jpeg becomes jpg
change the permissions of the moved file to 644
All at one go.
If there are multiple prefix* files, the command should just fail silently.
I will initially like to do it at the command prompt with an option to add a cron job later. I mean, will the same zsh command/ script work in cron?
I am sure, this is doable. However, with my limited shell knowledge, could only achieve:
mv /tmp/folder1/prefix-*.JPG ~/Pictures/$(date +'%Y%m%d').jpg
Problems with my approach are many. It does not handle capitalization, does not take care of different extensions and does not address the permission issue.
How about this:
#!/bin/sh
FILES="/tmp/folder1/prefix*.jpg /tmp/folder1/prefix*.jpeg /tmp/folder1/prefix*.png h/tmp/folder1/prefix*.JPG /tmp/folder1/prefix*.PNG"
if [ $(ls $FILES | wc -l ) -gt 1 ]; then
exit 1
fi
if [ $(ls $FILES | grep -i '\.png$') ]; then
SUFF=png
else
SUFF=jpg
fi
DEST=$HOME/Pictures/$(date +'%Y%m%d').$SUFF
mv $FILES $DEST
chmod 644 $DEST

Mac Terminal /bin/settitle.sh: No such file or directory

I'm trying to create a bash script to change the titles of my terminal windows so I can identify what they are doing. I spent a few hours on this and cant figure it out. The idea is to be able to execute settitle NewTitle. Thank you.
This is my echo:$PATH. It looks like Users/klik/bin is there twice. Maybe that is the issue?
~ klik echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/klik/bin:/Users/klik/bin
This is the script which was created in textedit in plain text format.
#!/bin/bash
# settitle: set the Mac Terminal title
# usage: to set the titlebar to 'PLAY', type: settitle PLAY
echo -e "\033]0;${1}\007\c"
This is my bash_profile and bin file.
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
export PATH=$PATH:$HOME/bin
alias desk='cd ~/Desktop/'
alias down='cd ~/Downloads/'
alias github='cd ~/github/'
This is my ls -a output
Current directories
~ klik ls -l $HOME/bin | pbcopy
total 8
-rwx--x--x# 1 klik staff 147 Mar 9 21:39 settitle.sh
Try this:
echo -e "\033]0;FreddyFrog\007\c"
You need to use -e to turn on interpretation of escape characters. You can also use printf.
printf '\033]0;%s\007\015' "Hippo Croco Horror Pig"
This issue above was that the file was saved with .txt extension. I dont know why this was the case given the ls command showed a .sh ext. At any rate, this is the process I used for creating this script and and executing it.
Open Finder -> Applications->TextEdit in Mac.
Select New Document at bottom left.
From menu select Format -> Make Plain Text
Paste in this code:
#!/bin/sh
# settitle: set the Mac Terminal title
# usage: to set the titlebar to 'PLAY', type: settitle PLAY
echo "\033]0;${1}\007\c"
Thanks to Alvin Alexander for the code.
Still in TextEdit select menu File -> Save
Uncheck "If no extension is provided, use ".txt" "
When I chose my file name I saved it with no extension so i could just type the command settitle NewTitle without having to type the extension every time.
Note the folder the file is being saved to. It defaults to desktop on my machine.
Open Finder -> Go -> Go to Folder
Type in the path to your User Bin folder: mine was /Users/klik/bin
You can check to see if you have a User/bin folder by running: ls -l from your home directory.
If you don't have a bin folder in this directory you can create one by going to your $HOME directory and executing:
mkdir bin
To find out what is your home directory see this
You can then open the directory by executing:
open bin
This will open the folder in Finder.
Drag the script file you created into this folder.
Make sure the script is executable by executing the following command from the folder the file is in or by including the path to the file in name of file:
chmod +x <name of file>
Make sure that the script is in your executable $PATH by executing:
echo $PATH
You will get something like this:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/<you>/bin
If you dont see the path of your script, ie /Users/''/bin, then the script file is not in your executable path and you need to put it in your .bash_profile. Execute ls -l to see if you have a .bash_profile file.
ls -l
If you don't have one, make sure your are in your $HOME directory then create one by executing:
mkdir .bash_profile
Open your .bash_profile file in your default editor:
open .bash_profile
Or open with nano (to save and close nano see this link):
nano .bash_profile
Add the following line to the .bash_profile then save/close:
export PATH=$PATH:$HOME/bin
Exit the terminal to reset by executing:
exit
Open the terminal then type:
settitle <whateveryouwant>
I hope this saves someone some time. Thanks to Mark Setchell for his constructive help.

Apache Subversion pre-commit to restrict files

I'm new to Apache SVN and I need some help to use a pre-commit script to filter which files are being upload to my repository.
I was searching a lot and found this script on another question, but it didn't work for me.
#!/bin/bash
REPOS=$1
TXN=$2
AWK=/usr/bin/awk
SVNLOOK="/usr/bin/svnlook";
#Put all the restricted formats in variable FILTER
FILTER=".(sh|xls|xlsx|exe|xlsm|XLSM|vsd|VSD|bak|BAK|class|CLASS)$"
# Figure out what directories have changed using svnlook.
FILES=`${SVNLOOK} changed -t ${REPOS} ${TXN} | ${AWK} '{ print $2 }'` > /dev/null
for FILE in $FILES; do
#Get the base Filename to extract its extension
NAME=`basename "$FILE"`
#Get the extension of the current file
EXTENSION=`echo "$NAME" | cut -d'.' -f2-`
#Checks if it contains the restricted format
if [[ "$FILTER" == *"$EXTENSION"* ]]; then
echo "Your commit has been blocked because you are trying to commit a restricted file." 1>&2
echo "Please contact SVN Admin. -- Thank you" 1>&2
exit 1
fi
done
exit 0
If I try to use svnlook changed -t repodirectory it didn't work because had a missing subcommand.
I overwrote my pre-commit.tmpl but it didn't work, can someone help me?
First - seems you incorrectly use svnlook. It should has parameters:
svnlook changed ${REPOS} -t ${TXN}
-t means 'read from transaction' and TXN - transaction name itself.
Second - not sure if I understand correctly, but hook file should has name pre-commit not pre-commit.tmpl
Third - pre-commit should has correct rights. For tests try a+rwx
update. It is not easy to obtain transaction object for tests, but you can use svnlook -r <revision> <repositiry_path> and experiment on already commited revisions.