Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

defining ksh variables....

Status
Not open for further replies.

pmcmicha

Technical User
May 25, 2000
353
I am writing a script in ksh and I am attempting to do the following, which should be possible.....

#!/bin/ksh -p

if [[ -d /home/someone ]]
then
1=$(echo "Found")
else
1=$(echo "Invalid")
fi

if [[ -f /test/test1 ]]
then
2=$(echo "Found")
else
2=$(echo "Invalid")
fi


echo "Now checking for someones directory\t\c" ${1}
echo
echo "Now checking for test file\t\c" ${2}


The problem is that the shell keeps giving me errors stating that 1=Found is not found. Same with 2. I know I could just write the echo statements and then do the test conditions, but I would like to seperate them this time to see how this looks. Is this possible? Thanks.
 
OK. A couple of things.

1) It's generally bad practice to use digits as variable names. Remember that arguments passed to a shell script are referred to as $1, $2, $3, ....

2) You don't need to echo a value to a shell variable. Just assign it as with any other language.

3) You don't need the double braces around your if condition.

4) The -c in the echo was causing a problem with the display of the output. It's not really needed any. You can force echo to take a new line using \n , otherwise a single echo statement will display all its arguments on one line.

Given the above comments, your script would now read
Code:
#!/bin/ksh

if [ -d /home/someone ]
then
      DIR="Found"
else
      DIR="Invalid"
fi

if [ -f /test/test1 ]
then
      FNAME="Found"
else
      FNAME="Invalid"
fi


echo "Now checking for someones directory\t${DIR}"
echo
echo "Now checking for test file\t${FNAME}"
There's still plenty of room for improvement though :) Just post back if you have any more queries.

Greg.
 
Thanks for the input. Just so you know, I don't use numbers as variables in my scripts, though when I am typing out an example I use them sometimes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top