How to expand to current remote path in custom command prompt in WinSCP? - winscp

I tried to make a custom command that unzip a selected file to a path defined by user, but with a default value to current path and current archive name in remote server using this command, but the prompt just gave me an empty value. What's the mistake?
unzip "!" -d "!?&Extraction Path:?!/!!"
Thanks in advance!

It's not possible. There's actually a feature request for this functionality:
Bug 743 – Allow patterns in default prompt answer in custom commands.
Though even that it meant to support only static (non-file) patterns like !/, but not file patterns like !.
If it helps, in WinSCP extensions, it's possible to use non-file patterns, like !/ (but not file patterns, like !) in default prompt/option answer.
The extension file may look like:
#name Unzip...
#side Remote
#command unzip "!" -d "%ExtractionPath%"
#option ExtractionPath -run textbox "Extraction path:" "!/"
Just store the above script to a text file and install it to WinSCP.
Another thing that you can do, is to add a checkbox that will make WinSCP add an archive name (without an extension) to the path, with some clever use of shell (bash) constructs. This way, you can uncheck the checkbox and add a custom subfolder to the target path manually, if you do not want to use the archive name for the subfolder name.
#name Unzip...
#side Remote
#command unzip "!" -d "%ExtractionPath%`[[ '%AddName%' = '1' ]] && AN=! && echo ${AN%.*}`"
#option ExtractionPath -run textbox "Extraction path:" "!/"
#option AddName -run checkbox "Add file name to the extraction path" "1" "1"
Yet another alternative is to use your own placeholder for archive name (e.g. ARCHIVENAME) that will get replaced by real name (without an extension), when the command is executed. Then, if you do not want to use the archive name for the subfolder name, you replace the ARCHIVENAME with a custom name.
#name Unzip...
#side Remote
#command unzip "!" -d "`EP=%ExtractionPath%;AN=!;AN=${AN%.*};echo ${EP/ARCHIVENAME/$AN}`"
#option ExtractionPath -run textbox "Extraction path:" "!/ARCHIVENAME"

Related

Coping ClearCase with label history

I got a task to copy files with certain extensions from clear case while I need to :
find all files with certain extension and their map
copy the mapping but replace the file with a dir that has it's name
copy the file labels history to that dir
So I know what do to separately but can't figure how to connect things:
Code I used :
# for the latest label :
find . -name '*.extension' | cpio -pdm /path/to/save # this helped me to copy all files and their dir map
# to copy all labels for that file
\cp -r filename.extension##/main/ /path/to/save # the ##main/ gives me the view of the labels
and their map
The "map file" is more seen on Windows with a type manager
The map file, located in the ccase-home-dir\lib\mgrs directory, associates type manager methods with the programs that carry them out.
A map file entry has three fields: type manager, method, and program.
On Linux:
On UNIX, and Linux a type manager is a collection of programs in a subdirectory of ccase-home-dir /lib/mgrs; the subdirectory name is the name by which the type manager is specified with the –manager option in a mkeltype command.
Each program in a type manager subdirectory implements one method (data-manipulation operation).
A method can be a compiled program, a shell script, or a link to an executable.
This differs from your "dir map".
You can list labels on the current version with: cleartool descr -fmt "%l" myFile.
Using extended paths can work in a dynamic view, but it is best (to get all labels on all branches) to do a:
cleartool find . -version "!lbtype(x)" -name "yourelement" -exec "cleartool descr -fmt \"%n labels:%l\n\" \"%CLEARCASE_XPN%\""
To combine both code, do a loop on the result of the first find command.
# Make sure globstar is enabled
shopt -s globstar
for i in **/*.extension; do # Whitespace-safe and recursive
cpio -pdm "${i}" /path/to/save
cp -r "${i}"##/main/ /path/to/save
done

Is there a way that i can download files from different URL's based on user input?

There are two releases
1. Dev available at https://example.com/foo/new-package.txt
2. GA available at https://example.com/bar/new-package.txt
I want the user to enter his choice of Dev or GA and based on that need to download the files, in the shell script is there a better way to do it?
There is a file which has environment variables that I'm sourcing inside another script.
env_var.sh
#!/bin/bash
echo "Enter your release"
export release='' #either Dev or GA
This file will be sourced from another script as
download.sh
#!/bin/bash
. ./env_var.sh #sourcing a environment var file
wget https://<Dev or GA URL>/new-package.txt
My main problem is how to set the wget URL based on the release set in env_var file.
Any help is appreciated.
Have you considered using read to get the user input?
read -p 'Selection: ' choice
You could then pass ${choice} to a function that has case statements for the urls:
get_url() {
case $1 in
'dev' ) wget https://example.com/foo/new-package.txt ;;
'ga' ) wget https://example.com/bar/new-package.txt ;;
\? ) echo "Invalid choice" ;;
esac
}
For more information on read, a good reference is TLDP's guide on user input.
Edit: To source a config file, run the command source ${PATH_TO_FILE}. You would then be able to pass the variable to the get_url() function for the same result.

How to create an alias with relative pwd + string

I want to set an alias to switch from two WordPress instances on the CLI. Each of them have the same paths except for the names of their respective sites e.g:
srv/deployment/work/sitename1/wp-content/uploads/2018/
srv/deployment/work/sitename2/wp-content/uploads/2018/
How do I create an alias that takes the "pwd" of the current location and cd
s to exactly the same location on the other site?
How about a bash function instead of an alias, gives you a little more freedom.
Save this bash function to a file like switchsite.sh. Modify the variables to your needs. Then load it into your bash with:
source switchsite.sh
If you are in /srv/deployment/work/sitename1/wp-content/uploads/2018, do
switchsite sitename2
and you will be in /srv/deployment/work/sitename2/wp-content/uploads/2018.
switchsite() {
# modify this to reflect where your sites are located, no trailing slash
where_my_sites_are=/srv/deployment/work
# modify this so it includes all characters that can be in a site name
pattern_matching_sitenames=[a-z0-9_\-]
# this is the first argument when the function is called
newsite=$1
# this replaces the site name in the current working directory
newdir=$(pwd | sed -n -e "s#\($where_my_sites_are\)/\($pattern_matching_sitenames\+\)/\(.*\)#\1/$newsite/\3#p")
cd $newdir
}
How it works: The sed expression splits the output of pwd into three parts: what is before the current site name, the current site name, and what comes after. Then sed puts it back together with the new site name. Just make sure the pattern can match all characters that could be in your site name. Research character classes for details.
Add the below lines into ~/.bash_aliases
export sitename1=srv/deployment/work/sitename1/wp-content/uploads/2018/
export sitename2=srv/deployment/work/sitename2/wp-content/uploads/2018/
After that
source ~/.bash_aliases
Then you can simply type sitename1 and sitename2 from anywhere to switch to respective directories

Searching in Finder from Selection

Usually I get an excel spreadsheet with dozens of filenames, for which I then need to go and search individually.
Spreadhseet
Is there a way that I could simply:
Select All filenames in e.g. row A of Excel,
then Search for all these files on "This Mac"
then Copy all found files into the New Folder on the Desktop
So far I've tried the first part of searching and this is what i get :a)
Automator with Variable. But the problem is, it only searches for 1 file from selection
b)
Automator with Shell Script (Copy to Clipboard > Open Finder > CMD+F (to highlight Search dialog) > CMD+V). It opens a new Finder window, but it doesn't paste the clipboard into search dialog
c) /usr/bin/pbcopy
on run {input, parameters}
tell application "System Events"
keystroke "f" using {command down}
keystroke "v" using {command down}
end tell
return input
end run`
End result, is same as option b). I was planning to run this in Automator as a 'Service', which I could later assign to Keyboard Shortcut.
I am pretty sure there should be a simple shell option for this - any advice would be much appreciated.
I made a bash script that does what you want. You would basically select a bunch of filenames in Excel, or any other app, and copy them to the clipboard with ⌘C. After that you need to run the script and it will take items from the clipboard and search for TIFF or JPEG images that match that name and copy them to a directory on your Desktop called Selected Files:
#!/bin/bash
# Get contents of clipboard into bash array
files=( $(pbpaste) )
# Create output directory - no checks for already existing or already containing files
OUTDIR="$HOME/Desktop/Selected Files"
mkdir -p "$OUTDIR"
# Iterate through fetching files
for ((i=0;i<${#files[#]};i++)) ; do
name=${files[i]}
result=$( mdfind "kMDItemDisplayName == \"${name}.*\" && (kMDItemKind==\"TIFF image\" || kMDItemKind==\"JPEG image\")" )
if [ -f "$result" ]; then
echo $name: $result
cp "$result" "$OUTDIR"
else
echo ERROR: Searched for: $name, found $result
fi
done
I am not sure of your level of familiarity with bash, so you may be able to ignore the following...
Make a new directory for your own scripts:
mkdir -p $HOME/scripts
Save the above script in that directory with filename:
$HOME/scripts/gather
Make the script executable by typing this into Terminal:
chmod +x $HOME/scripts/gather
Edit your login profile ($HOME/.profile) and add your $HOME/scripts directory to your PATH:
export PATH="$PATH":$HOME/scripts
Then start a new Terminal and you can use any script that you have saved in $HOME/scripts without needing to specify the full path to it, e.g.:
gather
Following information kindly contributed by #user3439894 in comments section, as I am out of my depth on this aspect...
To use a keyboard shortcut, you'd have to create an Automator "Service workflow" with a "Run Shell Script" action, which you can assign a keyboard shortcut to under: System Preferences > Keyboard > Shortcuts > Services

add content to the end of a .js file via ssh command

I'm trying to figure out how to add content/code to the end of a .js file that already has code in it using ssh command.
ie....
touch ./ap/includes/ckeditor/ckeditor.js
Maintain current code
echo "add custom end code only"> ./ap/includes/ckeditor/ckeditor.js
sshcommand is used to connect to another server.
What you can do append text to the end of a file is to echo "something" >> /your/file.
So based on your code:
touch ./ap/includes/ckeditor/ckeditor.js
Maintain current code
echo "add custom end code only" >> ./ap/includes/ckeditor/ckeditor.js
^
|_ changed this
By the way, the touch part is unnecessary. When echoing inside the file, the date of the file will be updated. And if file does not exist, it will be automatically created with echo.

Resources