summaryrefslogtreecommitdiff
path: root/Unix_Concepts_And_Applications_by_S_Das/21-Advanced_Shell_Programming.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Unix_Concepts_And_Applications_by_S_Das/21-Advanced_Shell_Programming.ipynb')
-rw-r--r--Unix_Concepts_And_Applications_by_S_Das/21-Advanced_Shell_Programming.ipynb564
1 files changed, 564 insertions, 0 deletions
diff --git a/Unix_Concepts_And_Applications_by_S_Das/21-Advanced_Shell_Programming.ipynb b/Unix_Concepts_And_Applications_by_S_Das/21-Advanced_Shell_Programming.ipynb
new file mode 100644
index 0000000..42d9b84
--- /dev/null
+++ b/Unix_Concepts_And_Applications_by_S_Das/21-Advanced_Shell_Programming.ipynb
@@ -0,0 +1,564 @@
+{
+"cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Chapter 21: Advanced Shell Programming"
+ ]
+ },
+{
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 21.1: Program_1.sce"
+ ]
+ },
+ {
+"cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+"source": [
+"clear\n",
+"flag=1\n",
+"mode(-1)\n",
+"clc\n",
+"printf('Example 1 : Show the method of using arrays in advanced shellscripting \n')\n",
+"disp('****************************************************************')\n",
+"disp('Answer : ')\n",
+"disp('INSTRUCTIONS : ')\n",
+"printf('\n1. Here all instructions are preloaded in the form of a demo\n\nInitially the whole perl script is displaying and then \n the result of the same can be seen in the command line interpreter.\n\n2. PLEASE MAKE SURE THAT THE PERLSCRIPT INTERPRETER\nEXISTS IN THE SYSTEM\nOR THE COMMAND WOULD NOT WORK \n\n3. PRESS ENTER AFTER EACH COMMAND to see its RESULT\n\n5. PRESS ENTER AFTER EACH RESULT TO GO TO THE NEXT COMMAND\n')\n",
+"halt('.............Press [ENTER] to continue.....')\n",
+"halt('')\n",
+"clc\n",
+"printf('\tUNIX SHELL SIMULATOR(DEMO VERSION WITH PRELOADED COMMANDS)\n\n\n')\n",
+"halt('')\n",
+"clc\n",
+"i=0\n",
+"i=i+1;f(i)='#!/usr/bin/ksh'\n",
+"i=i+1;f(i)='# Script: dateval.sh - validates a date field using an array'\n",
+"i=i+1;f(i)='IFS='+ascii(34)+'/'+ascii(34)+''\n",
+"i=i+1;f(i)='n='+ascii(34)+'[0-9][0-9]'+ascii(34)+''\n",
+"i=i+1;f(i)='set -A month arr 0 31 29 31 30 31 30 31 30 31 30 31'\n",
+"i=i+1;f(i)='while echo '+ascii(34)+'Enter a date: \c'+ascii(34)+' ; do'\n",
+"i=i+1;f(i)=' read value'\n",
+"i=i+1;f(i)=' case '+ascii(34)+'$value'+ascii(34)+' in'\n",
+"i=i+1;f(i)=' '+ascii(34)+''+ascii(34)+') echo '+ascii(34)+'No date entered'+ascii(34)+' ; continue ;;'\n",
+"i=i+1;f(i)=' $n/$n/$n) set $value'\n",
+"i=i+1;f(i)=' let rem='+ascii(34)+'$3 % $4'+ascii(34)+''\n",
+"i=i+1;f(i)=' if [ $2 -gt 12 -o $2 -eq 0 ] ; then'\n",
+"i=i+1;f(i)=' echo '+ascii(34)+'Illegal month'+ascii(34)+' ; continue'\n",
+"i=i+1;f(i)=' else'\n",
+"i=i+1;f(i)=' case '+ascii(34)+'$value'+ascii(34)+' in'\n",
+"i=i+1;f(i)=' 29/02/??) [ $rem -gt 0 ] &&'\n",
+"i=i+1;f(i)=' { echo '+ascii(34)+'20$3 is not a leap year'+ascii(34)+' ; continue ; } ;;'\n",
+"i=i+1;f(i)=' *) [ $1 -gt ${month_arr[$2]} -o $1 -eq 0 ] &&'\n",
+"i=i+1;f(i)=' { echo '+ascii(34)+'Illegal day'+ascii(34)+' ; continue ; } ;;'\n",
+"i=i+1;f(i)=' esac'\n",
+"i=i+1;f(i)=' fi;;'\n",
+"i=i+1;f(i)=' *) echo '+ascii(34)+'Invalid date'+ascii(34)+' ; continue ;;'\n",
+"i=i+1;f(i)=' esac'\n",
+"i=i+1;f(i)=' echo '+ascii(34)+'$1/$2/$3'+ascii(34)+' is a valid date'\n",
+"i=i+1;f(i)='done'\n",
+"n=i\n",
+"printf('\n# Enter the name of the shellscript file whichever you desire \n\n')\n",
+"nam=input('$ cat ','s')\n",
+"halt(' ')\n",
+"for i=1:n\n",
+" printf('%s\n',f(i))\n",
+"end\n",
+"halt(' ')\n",
+"clc\n",
+"i=0\n",
+"i=i+1;f(i)='@echo off'\n",
+"i=i+1;f(i)='set chc=y'\n",
+"i=i+1;f(i)=':loop'\n",
+"i=i+1;f(i)='if /I '+ascii(34)+'%chc%'+ascii(34)+'=='+ascii(34)+'n'+ascii(34)+' goto endloop'\n",
+"i=i+1;f(i)='set /P dat=Enter a date: '\n",
+"i=i+1;f(i)='if '+ascii(34)+'%dat%'+ascii(34)+' equ '+ascii(34)+''+ascii(34)+' echo No date entered&&goto chci'\n",
+"i=i+1;f(i)='if exist testt del testt'\n",
+"i=i+1;f(i)='echo %dat%>testt'\n",
+"i=i+1;f(i)='for /F '+ascii(34)+'tokens=1,2,3 delims=/'+ascii(34)+' %%i in (testt) do set dd=%%i&&set mm=%%j&&set yy=%%k'\n",
+"i=i+1;f(i)='if %mm% gtr 12 echo Illegal month&&goto chci'\n",
+"i=i+1;f(i)='if '+ascii(34)+'%mm%'+ascii(34)+'=='+ascii(34)+'01'+ascii(34)+' set ulim=31&&goto printing'\n",
+"i=i+1;f(i)='if '+ascii(34)+'%mm%'+ascii(34)+'=='+ascii(34)+'03'+ascii(34)+' set ulim=31&&goto printing'\n",
+"i=i+1;f(i)='if '+ascii(34)+'%mm%'+ascii(34)+'=='+ascii(34)+'04'+ascii(34)+' set ulim=30&&goto printing'\n",
+"i=i+1;f(i)='if '+ascii(34)+'%mm%'+ascii(34)+'=='+ascii(34)+'05'+ascii(34)+' set ulim=31&&goto printing'\n",
+"i=i+1;f(i)='if '+ascii(34)+'%mm%'+ascii(34)+'=='+ascii(34)+'06'+ascii(34)+' set ulim=30&&goto printing'\n",
+"i=i+1;f(i)='if '+ascii(34)+'%mm%'+ascii(34)+'=='+ascii(34)+'07'+ascii(34)+' set ulim=31&&goto printing'\n",
+"i=i+1;f(i)='if '+ascii(34)+'%mm%'+ascii(34)+'=='+ascii(34)+'08'+ascii(34)+' set ulim=31&&goto printing'\n",
+"i=i+1;f(i)='if '+ascii(34)+'%mm%'+ascii(34)+'=='+ascii(34)+'09'+ascii(34)+' set ulim=30&&goto printing'\n",
+"i=i+1;f(i)='if '+ascii(34)+'%mm%'+ascii(34)+'=='+ascii(34)+'10'+ascii(34)+' set ulim=31&&goto printing'\n",
+"i=i+1;f(i)='if '+ascii(34)+'%mm%'+ascii(34)+'=='+ascii(34)+'11'+ascii(34)+' set ulim=30&&goto printing'\n",
+"i=i+1;f(i)='if '+ascii(34)+'%mm%'+ascii(34)+'=='+ascii(34)+'12'+ascii(34)+' set ulim=31&&goto printing'\n",
+"i=i+1;f(i)='set /a rem=yy%%4'\n",
+"i=i+1;f(i)='if %rem% neq 0 set ulim=28&&goto nlpyear'\n",
+"i=i+1;f(i)='set ulim=29'\n",
+"i=i+1;f(i)='goto printing'\n",
+"i=i+1;f(i)=':nlpyear'\n",
+"i=i+1;f(i)='if '+ascii(34)+'%dd%'+ascii(34)+'=='+ascii(34)+'29'+ascii(34)+' echo 20%yy% is not a leap year&&goto chci'\n",
+"i=i+1;f(i)=':printing'\n",
+"i=i+1;f(i)='if %dd% leq %ulim% echo %dat% is a valid date&&goto chci'\n",
+"i=i+1;f(i)='echo Illegal day '\n",
+"i=i+1;f(i)=':chci'\n",
+"i=i+1;f(i)='set /p chc=Do you want to continue ? (y/n) : ' \n",
+"i=i+1;f(i)='goto loop'\n",
+"i=i+1;f(i)=':endloop'\n",
+"i=i+1;f(i)='pause>NUL&&del testt'\n",
+"n=i\n",
+"if getos()=='Linux' then\n",
+" printf('\n\nPlease Switch to windows and then execute using the instructions\n\nThank You \n\n')\n",
+" halt(' ')\n",
+" exit\n",
+"end\n",
+"v=mopen(nam+'.sh.bat','wt')\n",
+"for i=1:n\n",
+" mfprintf(v,'%s\n',f(i))\n",
+"end\n",
+"mclose(v)\n",
+"printf('\n# type the following command in the command line interpreter as soon as it appears')\n",
+"printf(' \n %c %s.sh %c [COMMANDLINE ARGUMENTS][ENTER]\n\n',ascii(34),nam,ascii(34))\n",
+"printf('\n$ %s.sh [COMMANDLINE ARGUMENTS] #to execute the perlscript',nam)\n",
+"halt(' ')\n",
+"dos('start')\n",
+"printf('\n\n\n')\n",
+"halt(' ---------------->Executing ShellScript in Command Line Prompt<-------------- ')\n",
+"printf('\n\n\n$ exit #To exit the current simulation terminal and return to Scilab console\n\n')\n",
+"halt('........# (hit [ENTER] for result)')\n",
+"//clc()\n",
+"printf('\n\n\t\t\tBACK TO SCILAB CONSOLE...\nLoading initial environment')\n",
+"sleep(1000)\n",
+"mdelete(nam+'.sh.bat')\n",
+"mdelete('emp.lst')"
+ ]
+ }
+,
+{
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 21.2: Program_2.sce"
+ ]
+ },
+ {
+"cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+"source": [
+"clear\n",
+"flag=1\n",
+"mode(-1)\n",
+"clc\n",
+"printf('Example 2 : Show the method of calling functions in shellscripts \n')\n",
+"disp('****************************************************************')\n",
+"disp('Answer : ')\n",
+"disp('INSTRUCTIONS : ')\n",
+"printf('\n1. Here all instructions are preloaded in the form of a demo\n\nInitially the whole perl script is displaying and then \n the result of the same can be seen in the command line interpreter.\n\n2. PLEASE MAKE SURE THAT THE PERLSCRIPT INTERPRETER\nEXISTS IN THE SYSTEM\nOR THE COMMAND WOULD NOT WORK \n\n3. PRESS ENTER AFTER EACH COMMAND to see its RESULT\n\n5. PRESS ENTER AFTER EACH RESULT TO GO TO THE NEXT COMMAND\n')\n",
+"halt('.............Press [ENTER] to continue.....')\n",
+"halt('')\n",
+"clc\n",
+"printf('\tUNIX SHELL SIMULATOR(DEMO VERSION WITH PRELOADED COMMANDS)\n\n\n')\n",
+"halt('')\n",
+"clc\n",
+"i=0\n",
+"i=i+1;f(i)='#! /bin/sh'\n",
+"i=i+1;f(i)='# Script: user_passwd.sh - Uses a shell function'\n",
+"i=i+1;f(i)='#'\n",
+"i=i+1;f(i)='.mainfunc.sh # Script containing valid_string function'\n",
+"i=i+1;f(i)=''\n",
+"i=i+1;f(i)='valid_string() {'\n",
+"i=i+1;f(i)=' while echo '+ascii(34)+'$1 \c'+ascii(34)+' 1>&2 ; do'\n",
+"i=i+1;f(i)=' read name'\n",
+"i=i+1;f(i)=' case $name in'\n",
+"i=i+1;f(i)=' '+ascii(34)+''+ascii(34)+') echo '+ascii(34)+'Nothing entered'+ascii(34)+' 1>&2 ; continue ;;'\n",
+"i=i+1;f(i)=' *) if [ `expr '+ascii(34)+'$name'+ascii(34)+' : '+ascii(39)+'.*'+ascii(39)+'` -gt $2 ] ; then'\n",
+"i=i+1;f(i)=' echo '+ascii(34)+'Maximum $2 characters permitted'+ascii(34)+' 1>&2'\n",
+"i=i+1;f(i)=' else'\n",
+"i=i+1;f(i)=' break'\n",
+"i=i+1;f(i)=' fi ;;'\n",
+"i=i+1;f(i)=' esac'\n",
+"i=i+1;f(i)=' done'\n",
+"i=i+1;f(i)=' echo $name'\n",
+"i=i+1;f(i)='}'\n",
+"i=i+1;f(i)=''\n",
+"i=i+1;f(i)='user=`valid_string '+ascii(34)+'Enter your user-id :'+ascii(34)+' 16`'\n",
+"i=i+1;f(i)='stty -echo # Password not to be echoed'\n",
+"i=i+1;f(i)='password=`valid_string '+ascii(34)+'Enter your password:'+ascii(34)+' 5`'\n",
+"i=i+1;f(i)='stty echo # Turns on echoing facility'\n",
+"i=i+1;f(i)='echo '+ascii(34)+'\nYour user-id is $user and your password is $password'+ascii(34)+''\n",
+"n=i\n",
+"printf('\n# Enter the name of the shellscript file whichever you desire \n\n')\n",
+"nam=input('$ cat ','s')\n",
+"halt(' ')\n",
+"for i=1:n\n",
+" printf('%s\n',f(i))\n",
+"end\n",
+"halt(' ')\n",
+"clc\n",
+"i=0\n",
+"i=i+1;f(i)='#!/usr/bin/perl' \n",
+"i=i+1;f(i)='$username = &take_input('+ascii(34)+'Enter your user-id: '+ascii(34)+', '+ascii(34)+'16'+ascii(34)+') ;'\n",
+"i=i+1;f(i)='$password = &take_input('+ascii(34)+'Enter your password: '+ascii(34)+', '+ascii(34)+'5'+ascii(34)+','+ascii(34)+'noecho'+ascii(34)+') ;'\n",
+"i=i+1;f(i)='print '+ascii(34)+'\nYour user-id is $username and your password is $password\n'+ascii(34)+' ;'\n",
+"i=i+1;f(i)='sub take_input { '\n",
+"i=i+1;f(i)='my ($prompt,$len,$flag) = @_ ;' \n",
+"i=i+1;f(i)='while (1) { '\n",
+"i=i+1;f(i)='print('+ascii(34)+'$prompt'+ascii(34)+') ;'\n",
+"i=i+1;f(i)='use Term::ReadKey;'\n",
+"i=i+1;f(i)='ReadMode 2 if (@_==3);'\n",
+"i=i+1;f(i)='chop($name=<STDIN>); '\n",
+"i=i+1;f(i)='ReadMode 0 if (@_==3);'\n",
+"i=i+1;f(i)='if ( length($name) eq 0 ) { printf '+ascii(34)+'\nNothing entered\n'+ascii(34)+' ; next }'\n",
+"i=i+1;f(i)='if ( length($name) > $len) { printf '+ascii(34)+'Maximum %d characters permitted\n'+ascii(34)+' , $len ; next }'\n",
+"i=i+1;f(i)='last if $name =~ /\w/ ;'\n",
+"i=i+1;f(i)='}'\n",
+"i=i+1;f(i)='$name ;'\n",
+"i=i+1;f(i)='}'\n",
+"n=i\n",
+"v=mopen(nam+'.pl','wt')\n",
+"for i=1:n\n",
+" mfprintf(v,'%s\n',f(i))\n",
+"end\n",
+"mclose(v)\n",
+"i=0\n",
+"i=i+1;f(i)='@echo off'\n",
+"i=i+1;f(i)=nam+'.pl'\n",
+"i=i+1;f(i)='pause>NUL'\n",
+"i=i+1;f(i)='del '+nam+'.pl'\n",
+"n=i\n",
+"v=mopen(nam+'.bat','wt')\n",
+"for i=1:n\n",
+" mfprintf(v,'%s\n',f(i))\n",
+"end\n",
+"mclose(v)\n",
+"if getos()=='Linux' then\n",
+" printf('\n\nPlease Switch to windows and then execute using the instructions\n\nThank You \n\n')\n",
+" halt(' ')\n",
+" exit\n",
+"end\n",
+"printf('\n# type the following command in the command line interpreter as soon as it appears')\n",
+"printf(' \n %c %s %c[ENTER]\n\n',ascii(34),nam,ascii(34))\n",
+"printf('\n$ %s #to execute the perlscript',nam)\n",
+"halt(' ')\n",
+"dos('start')\n",
+"printf('\n\n\n')\n",
+"halt(' ---------------->Executing ShellScript in Command Line Prompt<-------------- ')\n",
+"printf('\n\n\n$ exit #To exit the current simulation terminal and return to Scilab console\n\n')\n",
+"halt('........# (hit [ENTER] for result)')\n",
+"//clc()\n",
+"printf('\n\n\t\t\tBACK TO SCILAB CONSOLE...\nLoading initial environment')\n",
+"sleep(1000)\n",
+"mdelete(nam+'.bat')\n",
+"mdelete('emp.lst')\n",
+"mdelete(nam+'.pl')"
+ ]
+ }
+,
+{
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 21.3: Program_3.sce"
+ ]
+ },
+ {
+"cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+"source": [
+"clear\n",
+"flag=1\n",
+"mode(-1)\n",
+"clc\n",
+"printf('Example 3 : Show the method of using eval in the shellscripts \n')\n",
+"disp('****************************************************************')\n",
+"disp('Answer : ')\n",
+"disp('INSTRUCTIONS : ')\n",
+"printf('\n1. Here all instructions are preloaded in the form of a demo\n\nInitially the whole perl script is displaying and then \n the result of the same can be seen in the command line interpreter.\n\n2. PLEASE MAKE SURE THAT THE PERLSCRIPT INTERPRETER\nEXISTS IN THE SYSTEM\nOR THE COMMAND WOULD NOT WORK \n\n3. PRESS ENTER AFTER EACH COMMAND to see its RESULT\n\n4. PRESS ENTER AFTER EACH RESULT TO GO TO THE NEXT COMMAND\n')\n",
+"halt('.............Press [ENTER] to continue.....')\n",
+"halt('')\n",
+"clc\n",
+"printf('\tUNIX SHELL SIMULATOR(DEMO VERSION WITH PRELOADED COMMANDS)\n\n\n')\n",
+"halt('')\n",
+"clc\n",
+"printf('\n# Enter the name of the shellscript file whichever you desire \n\n')\n",
+"nam=input('$ cat ','s')\n",
+"halt(' ')\n",
+"i=0\n",
+"i=i+1;f(i)='#!/bin/sh'\n",
+"i=i+1;f(i)='# Script: dentry2.sh - Uses eval and shell functions'\n",
+"i=i+1;f(i)='#'\n",
+"i=i+1;f(i)='trap '+ascii(39)+'echo '+ascii(34)+'Program interrupted'+ascii(34)+';exit'+ascii(39)+' HUP INT TERM'\n",
+"i=i+1;f(i)='. mainfunc.sh # Invokes functions valid_string() and anymore ()'\n",
+"i=i+1;f(i)=''\n",
+"i=i+1;f(i)='prompt1='+ascii(34)+'Employee id : '+ascii(34)+' ; prompt2='+ascii(34)+'Name : '+ascii(34)+'; prompt3='+ascii(34)+'Designation : '+ascii(34)+''\n",
+"i=i+1;f(i)='prompt4='+ascii(34)+'Department : '+ascii(34)+' ; prompt5='+ascii(34)+'Date birth : '+ascii(34)+';prompt6='+ascii(34)+'Basic pay : '+ascii(34)+''\n",
+"i=i+1;f(i)='rekord='\n",
+"i=i+1;f(i)=''\n",
+"i=i+1;f(i)='flname=`valid_string '+ascii(34)+'Enter the output filename: '+ascii(34)+' 8`'\n",
+"i=i+1;f(i)='while true ; do'\n",
+"i=i+1;f(i)=' while [ ${x:=1} -le 6 ] ; do # x first set to 1'\n",
+"i=i+1;f(i)=' eval echo \$prompt$x '+ascii(39)+'\\c'+ascii(39)+' 1>&2'\n",
+"i=i+1;f(i)=' read value$x'\n",
+"i=i+1;f(i)=' rekord='+ascii(34)+'${rekord}`eval echo \\$value$x`|'+ascii(34)+''\n",
+"i=i+1;f(i)=' x=`expr $x + 1`'\n",
+"i=i+1;f(i)=' done'\n",
+"i=i+1;f(i)=' echo '+ascii(34)+'$rekord'+ascii(34)+''\n",
+"i=i+1;f(i)=' anymore '+ascii(34)+'More entries to add'+ascii(34)+' 1&>2 || break'\n",
+"i=i+1;f(i)='done > $flname'\n",
+"i=i+1;f(i)=''\n",
+"i=i+1;f(i)='anymore() {'\n",
+"i=i+1;f(i)=' echo '+ascii(34)+'\n$1 ?(y/n) : \c'+ascii(34)+' 1>&2'\n",
+"i=i+1;f(i)=' read response'\n",
+"i=i+1;f(i)=' case '+ascii(34)+'$response'+ascii(34)+' in'\n",
+"i=i+1;f(i)=' y/Y) echo 1>&2 ; return 0 ;;'\n",
+"i=i+1;f(i)=' *) return 1 ;;'\n",
+"i=i+1;f(i)=' esac'\n",
+"i=i+1;f(i)='}'\n",
+"n=i \n",
+"for i=1:n\n",
+" printf('%s\n',f(i))\n",
+"end\n",
+"halt(' ')\n",
+"clc\n",
+"i=0\n",
+"i=i+1;f(i)='@echo off'\n",
+"i=i+1;f(i)='set /P flname=Enter the output filename: '\n",
+"i=i+1;f(i)='if exist %flname% del %flname%'\n",
+"i=i+1;f(i)='set response=y'\n",
+"i=i+1;f(i)=':loop'\n",
+"i=i+1;f(i)='if /I '+ascii(34)+'%response%'+ascii(34)+'=='+ascii(34)+'n'+ascii(34)+' goto endloop'\n",
+"i=i+1;f(i)='echo.'\n",
+"i=i+1;f(i)='set /P eid=Employee id : '\n",
+"i=i+1;f(i)='set /P nam=Name : '\n",
+"i=i+1;f(i)='set /P desig=Designation : '\n",
+"i=i+1;f(i)='set /P dept=Department : '\n",
+"i=i+1;f(i)='set /P dob=Date birth : '\n",
+"i=i+1;f(i)='set /P bas=Basic pay : '\n",
+"i=i+1;f(i)='echo.'\n",
+"i=i+1;f(i)='echo %eid%:%nam%:%desig%:%dept%:%dob%:%bas%:>>%flname%'\n",
+"i=i+1;f(i)='set /P response=More entries to add ?(y/n) : '\n",
+"i=i+1;f(i)='goto loop'\n",
+"i=i+1;f(i)=':endloop'\n",
+"i=i+1;f(i)='pause>NUL'\n",
+"i=i+1;f(i)='echo.'\n",
+"i=i+1;f(i)='echo.'\n",
+"i=i+1;f(i)='set /p res2= Do you want to see the file %flname%?(y/n) : '\n",
+"i=i+1;f(i)='if /I '+ascii(34)+'%res2%'+ascii(34)+'=='+ascii(34)+'n'+ascii(34)+' goto endd'\n",
+"i=i+1;f(i)='echo $ cat %flname%'\n",
+"i=i+1;f(i)='type %flname%'\n",
+"i=i+1;f(i)=':endd'\n",
+"i=i+1;f(i)='pause>NUL' \n",
+"n=i\n",
+"if getos()=='Linux' then\n",
+" printf('\n\nPlease Switch to windows and then execute using the instructions\n\nThank You \n\n')\n",
+" halt(' ')\n",
+" exit\n",
+"end\n",
+"v=mopen(nam+'.sh.bat','wt')\n",
+"for i=1:n\n",
+" mfprintf(v,'%s\n',f(i))\n",
+"end\n",
+"mclose(v)\n",
+"printf('\n# type the following command in the command line interpreter as soon as it appears')\n",
+"printf(' \n %c %s.sh %c[ENTER]\n\n',ascii(34),nam,ascii(34))\n",
+"printf('\n$ %s.sh #to execute the perlscript',nam)\n",
+"halt(' ')\n",
+"dos('start')\n",
+"printf('\n\n\n')\n",
+"halt(' ---------------->Executing ShellScript in Command Line Prompt<-------------- ')\n",
+"printf('\n\n\n$ exit #To exit the current simulation terminal and return to Scilab console\n\n')\n",
+"halt('........# (hit [ENTER] for result)')\n",
+"//clc()\n",
+"printf('\n\n\t\t\tBACK TO SCILAB CONSOLE...\nLoading initial environment')\n",
+"sleep(1000)\n",
+"mdelete(nam+'.sh.bat')\n",
+"mdelete('emp.lst')"
+ ]
+ }
+,
+{
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 21.4: Program_4.sce"
+ ]
+ },
+ {
+"cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+"source": [
+"clear\n",
+"flag=1\n",
+"mode(-1)\n",
+"clc\n",
+"printf('Example 4 : Show the method of using exec command to make many streams \n')\n",
+"disp('****************************************************************')\n",
+"disp('Answer : ')\n",
+"disp('INSTRUCTIONS : ')\n",
+"printf('\n1. Here all instructions are preloaded in the form of a demo\n\nInitially the whole perl script is displaying and then \n the result of the same can be seen in the command line interpreter.\n\n2. PLEASE MAKE SURE THAT THE PERLSCRIPT INTERPRETER\nEXISTS IN THE SYSTEM\nOR THE COMMAND WOULD NOT WORK \n\n3. PRESS ENTER AFTER EACH COMMAND to see its RESULT\n\n5. PRESS ENTER AFTER EACH RESULT TO GO TO THE NEXT COMMAND\n')\n",
+"halt('.............Press [ENTER] to continue.....')\n",
+"halt('')\n",
+"clc\n",
+"printf('\tUNIX SHELL SIMULATOR(DEMO VERSION WITH PRELOADED COMMANDS)\n\n\n')\n",
+"i=0\n",
+"i=i+1;f(i)='2233|a.k.shukla |g.m. |sales |12/12/52|6000'\n",
+"i=i+1;f(i)='9876|jai sharma |director |production |12/03/50|7000'\n",
+"i=i+1;f(i)='5678|sumit chakrobarty|d.g.m |marketing |19/04/43|6000'\n",
+"i=i+1;f(i)='2356|barun sengupta |director |personnel |11/05/47|7800'\n",
+"i=i+1;f(i)='5423|n.k. gupta |chairman |admin |30/08/56|5400'\n",
+"i=i+1;f(i)='1006|chanchal singhvi |director |sales |03/09/38|6700'\n",
+"i=i+1;f(i)='6213|karuna ganguly |g.m. |accounts |05/06/62|6300'\n",
+"i=i+1;f(i)='1265|s.n. dasgupta |manager |sales |12/09/63|5600'\n",
+"i=i+1;f(i)='4290|jayant Choudhary |executive |production|07/09/50|6000'\n",
+"i=i+1;f(i)='2476|anil aggarwal |manager |sales |01/05/59|5000'\n",
+"i=i+1;f(i)='6521|lalit chowdury |director |marketing |26/09/45|8200'\n",
+"i=i+1;f(i)='3212|shyam saksena |d.g.m |accounts |12/12/55|6000'\n",
+"i=i+1;f(i)='3564|sudhir Agarwal |executive |personnel |06/07/47|7500'\n",
+"i=i+1;f(i)='2345|j.b. saxena |g.m. |marketing |12/03/45|8000'\n",
+"i=i+1;f(i)='0110|v.k. agrawal |g.m. |marketing |31/02/40|9000'\n",
+"n=i\n",
+"printf('\n\n$ cat emp.lst # to open the file emp.lst')\n",
+"halt(' ')\n",
+"u=mopen('emp.lst','wt')\n",
+"for i=1:n\n",
+" mfprintf(u,'%s\n',f(i))\n",
+" printf('%s\n',f(i))\n",
+"end\n",
+"mclose(u)\n",
+"halt('')\n",
+"clc\n",
+"i=0\n",
+"i=i+1;f(i)='#!/bin/sh'\n",
+"i=i+1;f(i)='# Script: countpat.sh -- Uses exec to handle multiple files'\n",
+"i=i+1;f(i)='#'\n",
+"i=i+1;f(i)='exec > $2 # Open file 1 for storing selected lines'\n",
+"i=i+1;f(i)='exec 3> $3 # Open file 3 for storing patterns not found'\n",
+"i=i+1;f(i)='exec 4> $4 # Open file 4 for storing invalid patterns'\n",
+"i=i+1;f(i)=''\n",
+"i=i+1;f(i)='[ $# -ne 4 ] && { echo '+ascii(34)+'4 arguments required'+ascii(34)+' ; exit 2 ; }'\n",
+"i=i+1;f(i)=''\n",
+"i=i+1;f(i)='exec < $1 # Redirecting output'\n",
+"i=i+1;f(i)='while read pattern ; do '\n",
+"i=i+1;f(i)=' case '+ascii(34)+'$pattern'+ascii(34)+' in'\n",
+"i=i+1;f(i)=' ????) grep $pattern emp.lst ||'\n",
+"i=i+1;f(i)=' echo $pattern not found in file 1>&3 ;;'\n",
+"i=i+1;f(i)=' *) echo $pattern not a four-character string 1>&4 ;;'\n",
+"i=i+1;f(i)=' esac'\n",
+"i=i+1;f(i)='done'\n",
+"i=i+1;f(i)='exec > \/dev/tty # Redirects standard output back to terminal'\n",
+"i=i+1;f(i)='echo Job Over'\n",
+"n=i\n",
+"printf('\n# Enter the name of the shellscript file whichever you desire \n\n')\n",
+"nam=input('$ cat ','s')\n",
+"halt(' ')\n",
+"for i=1:n\n",
+" printf('%s\n',f(i))\n",
+"end\n",
+"halt(' ')\n",
+"clc\n",
+"i=0\n",
+"i=i+1;f(i)='@echo off'\n",
+"i=i+1;f(i)='for %%x in (%*) do set /a ccc+=1'\n",
+"i=i+1;f(i)='if %ccc% neq 4 echo 4 arguments required&&goto endd'\n",
+"i=i+1;f(i)='echo.'\n",
+"i=i+1;f(i)='echo -----------Creating file %1-----------------'\n",
+"i=i+1;f(i)='set chice=y'\n",
+"i=i+1;f(i)=':loop1'\n",
+"i=i+1;f(i)='if /I '+ascii(34)+'%chice%'+ascii(34)+'=='+ascii(34)+'n'+ascii(34)+' goto endloop1'\n",
+"i=i+1;f(i)='set /P inp=Enter the employee-id : '\n",
+"i=i+1;f(i)='echo %inp%>>%1'\n",
+"i=i+1;f(i)='if exist len del len'\n",
+"i=i+1;f(i)='echo.%inp%>len'\n",
+"i=i+1;f(i)='for /F '+ascii(34)+'usebackq'+ascii(34)+' %%i in ('+ascii(39)+'len'+ascii(39)+') do set len=%%~zi'\n",
+"i=i+1;f(i)='del len&&set /a len-=2'\n",
+"i=i+1;f(i)='if %len% neq 4 echo %inp% is not a four-character string>>%4&&goto chi'\n",
+"i=i+1;f(i)='if exist res del res'\n",
+"i=i+1;f(i)='findstr /B '+ascii(34)+'%inp%'+ascii(34)+' emp.lst>res'\n",
+"i=i+1;f(i)='for /F '+ascii(34)+'usebackq'+ascii(34)+' %%i in ('+ascii(39)+'res'+ascii(39)+') do set siz=%%~zi'\n",
+"i=i+1;f(i)='if %siz% equ 0 echo %inp% not found in file>>%3&&goto chi'\n",
+"i=i+1;f(i)='type res>>%2'\n",
+"i=i+1;f(i)=':chi'\n",
+"i=i+1;f(i)='set /P chice=Do you want to continue?(y/n) : '\n",
+"i=i+1;f(i)='cls&&goto loop1'\n",
+"i=i+1;f(i)=':endloop1'\n",
+"i=i+1;f(i)='set /P c1=Do you want to see the file %1?(y/n) : '\n",
+"i=i+1;f(i)='if /I '+ascii(34)+'%c1%'+ascii(34)+'=='+ascii(34)+'n'+ascii(34)+' goto endloop2'\n",
+"i=i+1;f(i)='type %1'\n",
+"i=i+1;f(i)=':endloop2'\n",
+"i=i+1;f(i)='set /P c2=Do you want to see the file %2?(y/n) : '\n",
+"i=i+1;f(i)='if /I '+ascii(34)+'%c2%'+ascii(34)+'=='+ascii(34)+'n'+ascii(34)+' goto endloop3'\n",
+"i=i+1;f(i)='type %2'\n",
+"i=i+1;f(i)=':endloop3'\n",
+"i=i+1;f(i)='set /P c3=Do you want to see the file %3?(y/n) : '\n",
+"i=i+1;f(i)='if /I '+ascii(34)+'%c3%'+ascii(34)+'=='+ascii(34)+'n'+ascii(34)+' goto endloop4'\n",
+"i=i+1;f(i)='type %3'\n",
+"i=i+1;f(i)=':endloop4'\n",
+"i=i+1;f(i)='set /P c4=Do you want to see the file %4?(y/n) : '\n",
+"i=i+1;f(i)='if /I '+ascii(34)+'%c4%'+ascii(34)+'=='+ascii(34)+'n'+ascii(34)+' goto endloop2'\n",
+"i=i+1;f(i)='type %4'\n",
+"i=i+1;f(i)=':endloop5'\n",
+"i=i+1;f(i)='pause>NUL&&del %1&&del %2&&del %3&&del %4&&del res'\n",
+"n=i\n",
+"if getos()=='Linux' then\n",
+" printf('\n\nPlease Switch to windows and then execute using the instructions\n\nThank You \n\n')\n",
+" halt(' ')\n",
+" exit\n",
+"end\n",
+"v=mopen(nam+'.sh.bat','wt')\n",
+"for i=1:n\n",
+" mfprintf(v,'%s\n',f(i))\n",
+"end\n",
+"mclose(v)\n",
+"printf('\n# type the following command in the command line interpreter as soon as it appears')\n",
+"printf(' \n %c %s.sh %c [COMMANDLINE ARGUMENTS][ENTER]\n\n',ascii(34),nam,ascii(34))\n",
+"printf('\n$ %s.sh [COMMANDLINE ARGUMENTS] #to execute the perlscript',nam)\n",
+"halt(' ')\n",
+"dos('start')\n",
+"printf('\n\n\n')\n",
+"halt(' ---------------->Executing ShellScript in Command Line Prompt<-------------- ')\n",
+"printf('\n\n\n$ exit #To exit the current simulation terminal and return to Scilab console\n\n')\n",
+"halt('........# (hit [ENTER] for result)')\n",
+"//clc()\n",
+"printf('\n\n\t\t\tBACK TO SCILAB CONSOLE...\nLoading initial environment')\n",
+"sleep(1000)\n",
+"mdelete(nam+'.sh.bat')\n",
+"mdelete('emp.lst')"
+ ]
+ }
+],
+"metadata": {
+ "kernelspec": {
+ "display_name": "Scilab",
+ "language": "scilab",
+ "name": "scilab"
+ },
+ "language_info": {
+ "file_extension": ".sce",
+ "help_links": [
+ {
+ "text": "MetaKernel Magics",
+ "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md"
+ }
+ ],
+ "mimetype": "text/x-octave",
+ "name": "scilab",
+ "version": "0.7.1"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}