A JeDi Journey (there is no certainty, only opportunity)

yesterday padawan will become tommorow jedi

Posts Tagged ‘scripting

sh intro part 3

without comments

Just wondering about this line of code


dir 

echo "is it the same \$? = $? command completion status usage in windows and unix variant "

As I stated before in unix, each command can yield different result status. 0 for success and != 0 is for failed. But since my script will be running on the windows platform, the question is how do michaelsoft handle result status?? is it the same with unix ?? The answer is yes. ( lucky me ).

And sometimes u just interested with the result status of the command. So to discard the output for any command try this


dir > nul # for windows platform

ls > dev\null # for unix variant

what is this so called nul and dev\null. Its more like a blackhole if u ask me :d .

Written by peysal

April 11, 2008 at 10:14 pm

Posted in SofTware DevelopMent

Tagged with ,

simple sh intro part 2

without comments


echo "----------------------------";
echo "rm something yang tak wujud";
rm babunitam;
echo 'value $? patut bukan 0.. $?= ' $?;  #1
echo "----------------------------";
echo "running successful command :" ;ls;
echo '$? patut 0... $?=' $?; #1
echo "----------------------------";
echo "baca input dari user..saper nama awak:";
read input; #2
echo "nama anda adalah: $input"; #2
echo "----------------------------";
echo "total argument to this script \$#:$#"; #3
echo "actual argument to the script \$(from position \$1-\$9)*:$*"; #3
echo "script name:$0"; #4
echo "----------------------------";
echo "redirect error message to file"; rm sengal 2>error.txt; #5
exit 0; #6
  1. $? is a special variable that takes value from the previous command, whether the command success ( 0 ) or with some error message ( besides 0)
  2. read – to get input from user and put it to some variable
  3. $# is special variable for number of argument. $* is a special variable that will expand to its value from $1 – $9.
  4. $0 variable name that holds the name of the script.
  5. U can redirect the error message from console to a file using this method.
  6. exit 0 is where ur script run successful.

if [ $# -ne 2 ]
    then
        echo "error message coz argument not equal to 2" 1>&2
        exit 1;
    fi

The above code will redirect the stdout to stderr.

Written by peysal

April 8, 2008 at 7:52 am

Posted in SofTware DevelopMent

Tagged with ,

simple sh intro

with one comment

Last post, i did mentioned that i decided AWK not very suitable for my scripting purposes. After taking into consideration several factors on my choice of scripting language, i concluded my best choice at this moment is sh on unix utils. Actually eventhough the binary name of the file is sh.exe, but actually the implementation are based on zsh shell.

this is my sample code


#my first script
fullName="testst"; #user variable
echo 'my first shell script'
echo "today date is : "; date
echo "home directory: $HOME with its user is $USERNAME and fullnanme: $fullName";

echo "2 darab 2 (guna expr) ="`expr 2 \* 2`; #don do like this expr 2*2
exit 0;
  • take note that when u first assign variable, u dun need the $(dollar sign)
  • take extra precaution when using expr. Especially with the back quote and the space between operator.
  • “” != ” . In plain words, “” will caused the interpreter to evaluate the value inside the variable such as “$fullName”. While ‘$fullName’ will give u …well $fullName. In this context with echo command.

Written by peysal

April 7, 2008 at 10:51 am

Posted in SofTware DevelopMent

Tagged with ,