I want to make the file test condition a variable ($Prmshn in code below).
My goal is to use something like the first three unsuccessful if statetments since the 'if [[' is the newer style. Note: I copied the error message to the right of each of the three unsuccessful if statements.
Code:
#!/bin/ksh
test_input()
{
Prmshn=${1}
InFLNm=${2}
ifReq="-$Prmshn $InFLNm"
#the following three if statments fail:
#if [[ -${Prmshn} ${InFLNm} ]] ; then #/testing/test.ksh: syntax error at line 8 : `${InFLNm}' unexpected
#if [[ $ifReq ]] ; then #/testing/test.ksh: syntax error at line 9 : `]]' unexpected
#if [[ "$ifReq" ]] ; then #/testing/test.ksh: syntax error at line 10 : `]]' unexpected
#the following six if statments are successful:
#if [ "$ifReq" ] ; then
#if [[ -r /input/test.txt ]] ; then
#if [[ -r $InFLNm ]] ; then
#if test -r $InFLNm ; then
#if test -$Prmshn $InFLNm ; then
if test $ifReq ; then
# each of the previous if statements were used in turn with the balance of the statement following:
echo "input file is now available."
else
echo "input file is not available."
fi
}
test_input r /input/test.txt
You are setting a variable to start with the character "-".
Why are you expceting this to return true?
What are you expecting the user to pass?
Suggestion:
When labeling a variable like 'if', 'is', 'be' or 'do'-XY, i recomend that the variable is actualy a boolean.
So, when setting the variable, you take care that only a return value is stored, so you can check (use IF) it like you did.
Anyhow, for if blocks, i do not recomend 'test', though that is only personal preference, but to me the following are the same and tend to be most effective:
Code:
beBool=false
$beBool || echo "successfully failed"
[[ false = $beBool ]] && echo "successfully failed"
if [ false = $beBool ]
then echo "failed"
else echo "success"
fi
if ! $beBool
then echo "failed"
else echo "success"
fi
Ahhh i guess i understand (partly) what you want...
-${Prmshn} does NOT expand to: -P, -r, -m, -s, -h, -n
It expands to -'content_of_variable_Prmshn', which actualy is just what you passed as first argument $1.
As suggested by Rudi, execute your script with either -v or -x.
sea/RudiC - if i understand your suggestion correctly, i have called the test_input function with a -x parameter.
i still had similar results with the -x not resolving for the newist form of if statement (using the [[) however the -x resolves ok with the older forms of if statements (using [ and also test):
Code:
#!/bin/ksh
test_input()
{
Prmshn=${1}
InFLNm=${2}
# the following if stmt does not work (err msg to the right):
#if [[ ${Prmshn} ${InFLNm} ]] ; then # syntax error at line 7 : `${InFLNm}' unexpected
# the two following if stmts work ok:
#if [ "$Prmshn" "$InFLNm" ] ; then
if test "$Prmshn" "$InFLNm" ; then
echo "$InFLNm input file is now available."
else
echo "$InFLNm input file is not available."
fi
}
test_input -x /input/test.txt
The '-x' is not chaning anything of how it executes.
It just prints it before, so you have a way to see and check if the script does what it should.
As in, are there variables, and if there are, are they the right ones, and so on...
Write:
Code:
echo "DEBUG: $Prmshn -- $InFLNm"
above
Code:
if test "$Prmshn" "$InFLNm"
and run the script without '-x' and post its output please.
I'm new to unix and the command line and am trying to learn different commands. I have a file (teledir.txt) that contains a name and phone number for 3 different people. I am writing a script that is to take two positional parameters and I typed out how it should behave:
if <name and number... (1 Reply)
Please see part of a shell script below.
#Move folder to HOT folder
echo " moving $PRE_OCR_DIR/$BATCHNAME to $HOT_FOLDER_DIR\n"
#chmod -R 777 $PRE_OCR_DIR/$BATCHNAME
mv $PRE_OCR_DIR/$BATCHNAME $HOT_FOLDER_DIR
I wish to write an if statement around the move
that if that if $BATCHNAME... (3 Replies)
Hi,
Is there a way to compare the value in if condition with a list of values.
eg . if ]
then
echo "it's a mammal"
else
echo "its not"
fi
Thanks! (8 Replies)
sorry but need help
http://i.investopedia.com/inv/articles/site/CalculationEMA.gif
trying to achieve ema
in script I have this syntax which errors
ema=` ;
the 0.153846154 ='s Smoothing Factor
really appreciate help (3 Replies)
Hi,
I am having a shell Script which i need to modify. I am new to shell scripting , needs help. The Logic which needs to be incorporated is as follows :-
The script need to check the length of first Line in a data fine and of the length of the file is > 130 then validated the input... (2 Replies)
hello everyone
i am beginner on shell scripting .and i am working on my project work on ad hoc network
i wrote a batch (.sh) to do a looping and execute a tcl script i wrote before in each iteration ..but i got this problem "
syntax error near unexpected token `('... (1 Reply)
I'm working on a function in a shell script I'm writing that will eventually take in and print out a list of vendor names and aliases (for my work) Here's the function in question:
addvendorandalias ()
{
echo
echo -n 'Would you like to create a new vendor list (y or n)? '
read answer... (3 Replies)
Hi Guys,
iīve a question ... but itīs a litte bit tricky:
iīve a 3 php-scripts which runīs via cron at night. These script reads an xml-file a writes it in an MySQL-DB.
I named them (for example here ) Script1 - Script3. The XML-Files i named xml1 - xml3.
Now, iīve build a Batch-file,... (10 Replies)
Hi All,
I am new to shell scripting.
I have a variable which holds a numeric value.I have to check whether this variable holds a value between(0- 8),(8-17)(17-24).How do i write this syntax using if in shell scripting.
Thanks
Vignesh (2 Replies)
I have a rather big script that i have written in ksh and it is falling over in two places with a 'test argument' error. I know this usually means that the if statement is not correct, but it is fine. I have looked through the rest of the script for any odd brackets or ` marks, but can't see... (2 Replies)