set size 100 set idx 1 set remaining 1 while { $remaining > 0 } { set r [asset.query :size $size :idx $idx :count true :where "namespace>=/test"] foreach id [xvalues id $r] { puts $id } set idx [expr { $idx + $size }] set remaining [xvalue cursor/remaining $r] }
Search This Blog
Mediaflux TCL script: loop through query results with cursors
Python pysftp: bypass ssh host key verification
import pysftp cnopts = pysftp.CnOpts(); # set hostkeys to None to disable ssh key verification cnopts.hostkeys = None try: sftp = pysftp.Connection(host='sftp.yourdomain.org', username='sftp-user', password='password', port=22, cnopts=cnopts) print(sftp.pwd) finally: if sftp: sftp.close()
Windows Batch Script: remove trailing slash from a path string
set "a=C:\Users\wx\Downloads\" if "%a:~-1%"=="\" set "a=%a:~0,-1%" if "%a:~-1%"=="/" set "a=%a:~0,-1%" echo %a%
see also
Windows Batch Script: parse arguments
@echo off setlocal EnableExtensions EnableDelayedExpansion set verbose=false set value= :loop if "%~1"=="" ( if "%~1"=="--help" ( call :help exit /b 0 ) if "%~1"=="--value" ( set value=%~2 shift shift goto :loop ) if "%~1"=="--verbose" ( set verbose=true shift goto :loop ) shift goto :loop )
see also
Windows Batch Script: command argument expansion
expands %I removing any surrounding quotes:
%~I - expands %I removing any surrounding quotes (") %~fI - expands %I to a fully qualified path name %~dI - expands %I to a drive letter only %~pI - expands %I to a path only %~nI - expands %I to a file name only %~xI - expands %I to a file extension only %~sI - expanded path contains short names only %~aI - expands %I to file attributes of file %~tI - expands %I to date/time of file %~zI - expands %I to size of file %~$PATH:I - searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string
Windows Batch Script: string operations
- substring:
set "var=apple pear" echo %var~6,4% REM pear echo %var~0,-5% REM apple
- replace character:
set "var=a,b,c" REM replace commas with spaces echo %var:,= % REM a b c
see also
Windows Batch Script: comma in command arguments
Comma (or semicolon) in command arguments, if not quoted, are considered separators (same as white space). See the script (named test.bat) below:
if not quoted:
if quoted:
@echo off :loop if not "%~1"=="" ( echo %~1 shift goto :loop )When calling the script,
if not quoted:
test.bat 1,2,3
1 2 3
if quoted:
test.bat "1,2,3"
1,2,3
Windows Batch Script: enable delayed expansion
In compound code blocks, with setting EnableDelayedExpansion, the current value will NOT assigned to the variable as expected. see the code below:
set var=1 set switch=on if "%switch%"=="on" ( set var=2 echo %var% REM it prints 1 )It actually prints the original value 1. To avoid such unexpected behaviour, you need to set EnableDelayedExpansion, and using !var!:
setlocal EnableDelayedExpansion set var=1 set switch=on if "%switch%"=="on" ( set var=2 echo !var! REM it prints 2 )
see also
Windows Batch Script: split comma delimited string and loop through the tokens
@ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION SET emails=a@b.com,c@d.org,e@f.gov SET emails=%emails:,= % FOR %%e in (%emails%) DO ( echo %%e )
Windows Batch Script: replace a character in string
SET str=1,2,3,4 REM replace comma with blank space SET str=%str:,= % ECHO %str%
see also
Windows Batch Script: get the script file name
syntax
- ~ -- strip/trim quotes off
- d -- drive
- p -- path
- n -- file name
- x -- extension
- f -- full path
examples
suppose I have a script file at C:\Users\WXYZ\Documents\test.cmd@ECHO OFF REM test.cmd ECHO %~0 REM test ECHO %~n0 REM .cmd ECHO %~x0 REM C:\Users\WXYZ\test.cmd ECHO %~dpf0 REM C:\Users\WXYZ\test.cmd ECHO %~dpnx0 REM C:\Users\WXYZ\test ECHO %~dpn0 REM C:\Users\WXYZ\ ECHO %~dp0
see also
Windows Batch Script: IF THEN ELSE
ECHO TEST IF %ERRORLEVEL% EQU 0 ( ECHO success ) ELSE ( ECHO failure )
BASH script: split string to array
str="a,b,c,d" IFS=',' read -r -a array <<< "$str" for e in "${array[@]}" do echo $e done
see also
Shell scripting: extract substring (or path components) from string in BASH
str="I like shell scripting." echo ${str:7:5} # 7 is offset, 5 is length, it will print shell root=/a path=/a/b/c echo ${path#*/} # trim the prefix: a/b/c echo ${path#/a/} # trim the prefix: b/c echo ${path#$root/} # trim the prefix: b/c echo ${path#*b} # trim the prefix: /c name=/a/b/c.txt echo ${name%/*} # trim the suffix: /a/b echo ${name%.txt} # trim the suffix: /a/b/c
see also
Shell scripting: loop through an array or positional access
# declare a empty array declare -a array1=() array1+=('foo') array1+=('bar') echo ${array1[0]} # foo echo ${array1[1]} # bar for elem in "${array1[@]}" do echo $elem done
Shell scripting: append element to array
declare -a array1=() a+=('foo') a+=('bar') for elem in "${array1[@]}" do echo $elem done
Shell scripting: check if an array is empty
declare -a array1=() if [[ ${#array1[@]} -eq 0 ]]; then echo "array1 is empty" fi array1+=('foo') echo "array1 has ${#array1[@]} elements" array1+=('bar') echo "array1 has ${#array1[@]} elements"
Windows Batch Script: download file using bitsadmin without ssl certification validation
- For the web server that does not support content range requests. You have to set priority to FOREGROUND to force the job to run synchronously. Otherwise, error: BG_E_INSUFFICIENT_RANGE_SUPPORT (0x80200013)
bitsadmin /transfer MyJob /priority FOREGROUND "http://your-site.org/file.zip" "C:\test\file.zip"
- For the web server with self signed certificate or could not pass the ssl certificate validation, you cannot use a single bitsadmin /transfer command and the job has to be run asynchronously. You have to bitsadmin /create a job and bitsadmin /SetSecurityFlags to value 30
bitsadmin /create MyJob1 bitsadmin /addfile MyJob1 "https://your-site.org/file.zip" "C:\test\file.zip" bitsadmin /SetSecurityFlags MyJob1 30 bitsadmin /SetPriority MyJob1 FOREGROUND bitsadmin /resume MyJob1 bitsadmin /monitor || bitsadmin /cancel MyJob1
see also
curl and wget: bypass ssl certification validation
- --insecure or -k for curl:
curl --create-dirs -k -o /path/to/output-dir/file https://your-site.org/file
- --no-check-certificate for wget:
wget --no-check-certificate -O /path/to/output-dir/file https://your-site.org/file
curl and wget: create parent directories
curl --create-dirs -o "/path/to/output-dir/file.txt" "https://your-site.org/file.txt"
mkdir -p "/path/to/output-dir" && wget --no-check-certificate -O "/path/to/output-dir/file.txt" "https://your-site.org/file.txt"
Bash scripting: check whether the script is sourced or executed in subshell
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then echo "sourced" else echo "executed" fi
The example below, call return if the script is sourced otherwise call exit:
my_func1 || ( if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then return 1; else exit 1 )
see also
- https://groups.google.com/forum/#!topic/comp.unix.shell/gSa__gT81z0
Shell Script: if then else in one line
if [[ ${a} -ge 0 ]]; then echo "positive number"; else echo "negative number"; fi
my_func if [[ $? -ne 0 ]]; then echo "failure"; else echo "success"; fi
Windows Batch Script: Download file using powershell
SET "url=https://your-site.org/a-file.zip" SET "out=C:\Downloads" POWERSHELL -COMMAND "[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true};(New-Object Net.WebClient).DownloadFile('%url%', '%out%')"NOTE:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
bypass the SSL certificate validation.
See also
Windows Batch Script: call multiple powershell commands in one line
Multiple powershell commands are separated by semicolons:
POWERSHELL -COMMAND "[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true};(New-Object Net.WebClient).DownloadFile('%url%', '%out%')"
Windows Batch Script: conditional exit one line
CALL :FUNC1 || ( ECHO "Error: exit code=%ERRORLEVEL%" && EXIT /B 1 )
Windows Batch Script: Create directories recursively
SETLOCAL EnableExtensions SET dir="C:\A\B\C" MD "%dir%"
Windows Batch Script: dirname equivalent, get the parent directory of a file
Note: the code below can handle the path contains spaces:
- Option 1:
SET filename="C:\Files\A Dir\file1.txt" FOR %%F IN (%filename%) DO SET "dirname=%%~dpF" ECHO %dirname%
- Option 2:
SET "filename=C:\Files\A Dir\file1.txt" FOR %%F IN ("%filename%") DO SET "dirname=%%~dpF" ECHO %dirname%
Subscribe to:
Posts (Atom)