LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   AGE=`cat ${USERNAME}_DAT (https://www.linuxquestions.org/questions/linux-newbie-8/age%3D%60cat-%24%7Busername%7D_dat-924187/)

jayantbhagat 01-17-2012 04:39 AM

AGE=`cat ${USERNAME}_DAT
 
#!/bin/sh

# Prompt for a user name...
echo "Please enter your name:"
read USERNAME

# Check for the file.
if [ -s ${USERNAME}_DAT ]; then
# Read the age from the file.
AGE=`cat ${USERNAME}_DAT`
echo "You are $AGE years old!"
else
# Ask the user for his/her age
echo "How old are you?"
read AGE

if [ "$AGE" -le 2 ]; then
echo "You are too young!"
else
if [ "$AGE" -ge 100 ]; then
echo "You are too old!"
else
# Write the age to a new file.
echo $AGE > ${USERNAME}_DAT
fi
fi
fi




In this program what is AGE=`cat ${USERNAME}_DAT and how it work .....

corp769 01-17-2012 04:48 AM

Honestly, this seems like homework.... Can't you see what it is? This is a bash script, and just by looking at it, I can see what it is, and what it does.

David the H. 01-17-2012 09:06 AM

Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Actually, it's not a bash script. Since it begins with #!/bin/sh, and doesn't contain any bash-specific syntax, it's just a generic posix-compliant script.

Still, everything written there should be covered in just about any bourne-compatible shell tutorial out there. Here's my favorite:

http://mywiki.wooledge.org/BashGuide


BTW, $(..) is highly recommended over `..`. ;)

corp769 01-17-2012 12:38 PM

Oops, my fault. It's a shell script ;) Thanks for the correction though! I read it too fast.....

suicidaleggroll 01-17-2012 12:48 PM

Code:

AGE=`cat ${USERNAME}_DAT`
dumps the contents of the ${USERNAME}_DAT file (eg: Johnny_DAT) into the variable "AGE"

chrism01 01-17-2012 11:48 PM

To see what's going on, amend the top of the script
Code:

#!/bin/sh

set -xv

which will show you what the parser is doing.
Useful links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Note that 'sh' & 'bash' are different shells, but very similar.
To fully take advantage of those links, use bash instead of sh.


All times are GMT -5. The time now is 03:56 AM.