Monday, January 31, 2011

BASH : First Script example Hello World

echo hello world -> prints 'hello word'
pico helloworld.sh
CONTENT of file
#!/bin/bash - shel interpreter
clear
echo hello world
#end
ls -ltr -> shows the last file created at the bottom
source helloword.sh -> prints 'hello world'
source executes a 'sh' file even if it has no permisions
./helloworld -> Permission denied
echo $? - > result 126
'./' - means this directory
'tab' completion works for executable files, if the file is not executable than the 'tab' won't work
chmod u+x helloworld.sh
./helloword.sh -> prints 'helloworld'
MODIFY FILE helloworld.sh
clear 
echo "hello world $" -> prints all
echo "hello world $value" -> prints 'hello world'
echo 'hello world $value' -> prints 'hello world $value'


MESSAGE="hello world"
clear
echo '$MESSAGE' -> prints '$MESSAGE'
echo "$MESSAGE" -> prints 'hello world'
echo $MESSAGE -> prints 'hello world'
#clear -> comment line
we can run ./helloworld.sh | wc -> returns the number of lines, words, characters
./helloworld.sh > text.txt -> overrides the content of text.txt
./helloworld.sh >> text.txt -> appends the content to text.txt


echo What is your name?
read name -> read something from standard input
clear
echo Hello $name -> prints 'Hello $name'
echo How are you doing?
read feelingecho You said you were feeling $feeling


echo Script name: 'basename $0' -> $0-current script that is running. Result is 'Script name: helloword.sh'
echo Script name: $0 -> Result is 'Script name: ./helloword.sh'. You can see the entire path


echo You are curently in the directory $PWD;echo And the current local time is 'date +%r'
RESULT:
You are currently in directory /root
And the current local time is 11:53:21 PM


you can include in a script call of another script like '. helloworldx.sh'
touch helloworldx.sh
pico helloworldx.sh
MESSAGE3="we are testing includes"
echo $MESSAGE3


if you run ./hellowworld.sh you obtain the RESULT
You are currently in directory /root
And the current local time is 11:53:21 PM
we are testing includes


head -n 1 helloworld,sh -> Returns the first line of the script


another method include in a script call of another script like 'source helloworldx.sh' -> The results is the same like the one above


./helloworld.sh | mail -s "testing hellow world using pipes" root ->helloworld.sh send the output to the root user by email
mutt -> utilitary to see emails

No comments:

Post a Comment