<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <HTML> <HEAD> <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9"> <TITLE>BASH Programming - Introduction HOW-TO: Misc</TITLE> <LINK HREF="Bash-Prog-Intro-HOWTO-11.html" REL=next> <LINK HREF="Bash-Prog-Intro-HOWTO-9.html" REL=previous> <LINK HREF="Bash-Prog-Intro-HOWTO.html#toc10" REL=contents> </HEAD> <BODY> <A HREF="Bash-Prog-Intro-HOWTO-11.html">Next</A> <A HREF="Bash-Prog-Intro-HOWTO-9.html">Previous</A> <A HREF="Bash-Prog-Intro-HOWTO.html#toc10">Contents</A> <HR> <H2><A NAME="s10">10. Misc</A> </H2> <H2><A NAME="ss10.1">10.1 Reading user input with read</A> </H2> <P> In many ocations you may want to prompt the user for some input, and there are several ways to achive this. This is one of those ways: <BLOCKQUOTE><CODE> <PRE> #!/bin/bash echo Please, enter your name read NAME echo "Hi $NAME!" </PRE> </CODE></BLOCKQUOTE> <P> As a variant, you can get multiple values with read, this example may clarify this. <BLOCKQUOTE><CODE> <PRE> #!/bin/bash echo Please, enter your firstname and lastname read FN LN echo "Hi! $LN, $FN !" </PRE> </CODE></BLOCKQUOTE> <P> <H2><A NAME="ss10.2">10.2 Arithmetic evaluation</A> </H2> <P> On the command line (or a shell) try this: <P> echo 1 + 1 <P> If you expected to see '2' you'll be disappointed. What if you want BASH to evaluate some numbers you have? The solution is this: <P> echo $((1+1)) <P> This will produce a more 'logical' output. This is to evaluate an arithmetic expression. You can achieve this also like this: <P> echo $[1+1] <P> <P> If you need to use fractions, or more math or you just want it, you can use bc to evaluate arithmetic expressions. <P> if i ran "echo $[3/4]" at the command prompt, it would return 0 because bash only uses integers when answering. If you ran "echo 3/4|bc -l", it would properly return 0.75. <H2><A NAME="ss10.3">10.3 Finding bash </A> </H2> <P> From a message from mike (see Thanks to) <P> you always use #!/bin/bash .. you might was to give an example of <P> how to find where bash is located. <P> 'locate bash' is preferred, but not all machines have locate. <P> 'find ./ -name bash' from the root dir will work, usually. <P> Suggested locations to check: <P> ls -l /bin/bash <P> ls -l /sbin/bash <P> ls -l /usr/local/bin/bash <P> ls -l /usr/bin/bash <P> ls -l /usr/sbin/bash <P> ls -l /usr/local/sbin/bash <P> (can't think of any other dirs offhand... i've found it in <P> most of these places before on different system). <P> You may try also 'which bash'. <H2><A NAME="ss10.4">10.4 Getting the return value of a program</A> </H2> <P> In bash, the return value of a program is stored in a special variable called $?. <P> This illustrates how to capture the return value of a program, I assume that the directory <I>dada</I> does not exist. (This was also suggested by mike) <BLOCKQUOTE><CODE> <PRE> #!/bin/bash cd /dada &> /dev/null echo rv: $? cd $(pwd) &> /dev/null echo rv: $? </PRE> </CODE></BLOCKQUOTE> <H2><A NAME="ss10.5">10.5 Capturing a commands output </A> </H2> <P> This little scripts show all tables from all databases (assuming you got MySQL installed). Also, consider changing the 'mysql' command to use a valid username and password. <BLOCKQUOTE><CODE> <PRE> #!/bin/bash DBS=`mysql -uroot -e"show databases"` for b in $DBS ; do mysql -uroot -e"show tables from $b" done </PRE> </CODE></BLOCKQUOTE> <H2><A NAME="ss10.6">10.6 Multiple source files</A> </H2> <P> You can use multiple files with the command source. <P> __TO-DO__ <HR> <A HREF="Bash-Prog-Intro-HOWTO-11.html">Next</A> <A HREF="Bash-Prog-Intro-HOWTO-9.html">Previous</A> <A HREF="Bash-Prog-Intro-HOWTO.html#toc10">Contents</A> </BODY> </HTML>