NSIS restrict folder installation - nsis

I have a NSIS installer I am working on that I need to be able to prevent installation to the "user error" folders (i.e. $SYSDIR, $WINDIR, $DESKTOP etc... )
I want them to be able to chose a installation path but just have the next button be disabled if they chose a location as listed above. I have searched everywhere and can't find an answer to this one.
I was trying to use this but I can still install to desktop:
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE MyDirLeave
!insertmacro MUI_PAGE_DIRECTORY
...
Function MyDirLeave
Push $0
StrLen $0 $DESKTOP
StrCpy $0 $INSTDIR $0
StrCmp $0 $DESKTOP 0 proceed
MessageBox MB_ICONSTOP|MB_OK \
"Installation on DESKTOP is not allowed, choose another directory"
Abort
proceed:
Pop $0
FunctionEnd

Use the .onVerifyInstDir callback function.
Edit:
Function .onVerifyInstDir
StrLen $0 $Desktop
StrCpy $0 $INSTDIR $0
StrCmp $0 $Desktop 0 PathGood
Abort
PathGood:
FunctionEnd

Related

Rename files with different suffixes with only one new suffix

I am trying to modify a pipeline for viral taxonomy analysis. In this pipeline, the variables R1 and R2 are the two input paired-end fastq files. But theses files can be .fastq.gz or .fq.gz. After trimming of these fastq files with the program Trimgalore, the pipeline is coded to rename them by removing the suffix and adding an other. The pipeline can treat only .fq.gz files for the moment:
mv `basename $R1 .fq.gz`_val_1.fq $trimmedR1
mv `basename $R2 .fq.gz`_val_2.fq $trimmedR2
and I am trying to correct it:
#Command to execute:
if [[ "$R1" == *.fq.gz ]]
then
mv `basename $R1 .fq.gz`_val_1.fq $trimmedR1
mv `basename $R2 .fq.gz`_val_2.fq $trimmedR2
else
mv `basename $R1 .fastq.gz`_val_1.fq $trimmedR1
mv `basename $R2 .fastq.gz`_val_2.fq $trimmedR2
fi
but my command above is not correct and I don't know why. I also tried to use the command basename -a but it didn't work too. Thanks in advance to help me correct my code.

Best way to deal with passing arguments to nested script calls and creating a effective menu driven cli

I am writing a central script, which acts as a menu driven cli through which I trigger other scripts based on the input.
#!/bin/bash
echo "Server Name - `hostname`"
echo "-------------------------------"
echo " M A I N - M E N U"
echo "-------------------------------"
echo "1. init"
echo "2. insert"
echo "3. show"
echo "4. update"
echo "5. rm"
echo "6. ls"
echo "7. shutdown -> shutdown the server"
while :
do
printf '> '
read -r varname
set -- $varname
case $1 in
init)
./init.sh $2
echo $status
;;
insert)
./insert.sh $2 $3 '' $4
;;
show)
./show.sh $2 $3
;;
update)
./insert.sh $2 $3 'f' $4
;;
rm)
./rm.sh $2 $3
;;
ls)
./ls.sh $2 $3
;;
shutdown)
exit 0
;;
exit)
exit 0
;;
*)
#echo "Error: Bad request"
;;
esac
done
Current:
I have purposefully added the print '> ' statement so as to get an interactive interface just like node cli.
insert user1 google 'login:blahblah#gmail.com\npass:blah'
$1 is insert
$2 is user1
$3 is google
$4 is 'login:blahblah#gmail.com\npass:blah'
Expected:
I wish to read arguments the same way we pass arguments when we run a script.
Ex:
sh add.sh 10 20
or
sh add.sh "10" "20"
Either way when we read $1 or $2 in the add shell script, we will be getting 10 and 20 as their values.
insert user1 google 'login:blahblah#gmail.com\npass:blah'
$1 is insert
$2 is user1
$3 is google
$4 is login:blahblah#gmail.com\npass:blah
I do not want to get the input and then remove the trailing quotes. I would like to have the exact behaviour when we run a script and pass arguments.
PS: Please suggest how to get a cli just like node too which has access to previously run commands using the Up arrow.
Thanks in advance
The central idea here seems to revolve around these lines:
read -r varname
set -- $varname
The read command reads a whole line of input as literal text (without the possibility of line continuation) and records it as the value of variable varname, then that variable is expanded to provide arguments to the set builtin. But this is not equivalent to presenting the same command line as shell input.
Of particular relevance to the question is that quotation marks arising from variable expansion are not subject to quote removal. Also, brace and tilde expansion happen prior to variable expansion, so they will not be effective for you either.
What you really seem to want is a double expansion. Specifically, this:
eval "set -- $varname"
Do note that eval is very dangerous and subject to exploitation. For example, consider what would happen a user enters this command into your script (which is supposed running with sufficient privilege to perform a system shutdown):
show pwned; rm -rf /*

shell script sh removing slash (/) from argument

How can I prevent the slash from crashing my program. My first thought is to remove the slash but if there is a better method, please let me know. I don't know if the user will pass in "dir" or "dir/". This is in sh.
$1="directory/"
for i in "$1"/*
do
some code
done
You can remove it if present with a parameter expansion:
for i in "${1%/}"/*
which expands to $1 with the slash removed from the end.
I think you have a problem like #Benjamin said: Do not assign to $1.
Let's check the input and assign to dir.
#
if [ $# -eq 0 ]; then
echo "Usage: $0 directory [other parameters]"
exit 1
fi
if [ -d "$1"]; then
dir="${1%/}"
else
echo "$1 is not a valid directory"
exit 1
fi
for file in "$dir"/*
do
some code with "${file}"
done

How detect launch NSIS installer with arguments

I have NSIS installer.
How detect launch NSIS installer with arguments?
For example installer.exe /DEBUG
GetOptions:
!include FileFunc.nsh
!include LogicLib.nsh
Function .onInit
${GetParameters} $0
ClearErrors
${GetOptions} $0 "/DEBUG" $1
${IfNot} ${Errors}
MessageBox mb_ok "Debug"
${EndIf}
FunctionEnd

Given the file name, how to get it from a directory in shell

I want to compare 2 files with the same name in different directories.
$1 and $2 are 2 directories. I can check if there are same name files, but then i don't know how to get the 2nd file..
cd $1
for i in `ls`
do
if [ -f $2/$i ]
then
echo "find it in another directory"
GET THE OTHER FILE IN $2, THEN COMPARE THEM
cmp -s $i THE OTHER FILE
if [ $? = 0 ]
echo "they are same"
else
echo "they are different"
fi
fi
done
Simplest problem would be spaces in the args - easy to fix, just quote $1 and $2
if [ -f "$2/$i" ]
But I suspect the problem is that you are CDing into $1, which means $2 is invalid (if it is a relative path)
Solution1) Use absolute paths (e.g. /staff/bathpp/stuff/dir2)
Solution2) If you are expecting relative paths, then grab the current dir before jumping.
origDir=`pwd`
...
path2="$origDir/$path2"
Personally I'd to some checks so it worked for both.
for FIRST in $1/*
do
SECOND=$2/$(basename "$FIRST")
if [ -f "$SECOND" ]; then
diff --brief "$FIRST" "$SECOND"
fi
done
N.B. diff --brief only outputs when they are different. If you want to see the actual difference, remove the --brief.

Resources