diff options
Diffstat (limited to 'Unix_Concepts_And_Applications_by_S_Das/5-Handling_Ordinary_Files.ipynb')
-rw-r--r-- | Unix_Concepts_And_Applications_by_S_Das/5-Handling_Ordinary_Files.ipynb | 793 |
1 files changed, 793 insertions, 0 deletions
diff --git a/Unix_Concepts_And_Applications_by_S_Das/5-Handling_Ordinary_Files.ipynb b/Unix_Concepts_And_Applications_by_S_Das/5-Handling_Ordinary_Files.ipynb new file mode 100644 index 0000000..32a9cdf --- /dev/null +++ b/Unix_Concepts_And_Applications_by_S_Das/5-Handling_Ordinary_Files.ipynb @@ -0,0 +1,793 @@ +{ +"cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Chapter 5: Handling Ordinary Files" + ] + }, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.1: cat_command.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 file handling using the cat command \n')\n", +"disp('****************************************************************')\n", +"disp('Answer : ')\n", +"disp('INSTRUCTIONS : ')\n", +"printf('\nHere all instructions are preloaded in the form of a demo\nPRESS ENTER AFTER EACH COMMAND to see its RESULT\nPRESS 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", +"printf('\n# Enter the name of the file which you want to access \n\n')\n", +"nam=input('$ cat ','s')\n", +"printf('# This searches for a file named %s \n\n',nam)\n", +"if ~isfile(nam) then\n", +" flag=0\n", +" printf('\n%s : file not found \n',nam)\n", +" printf('# Create a new file named %s?\n # y : Yes \n # n : No \n',nam)\n", +" resp=input('','s')\n", +" if resp=='y' then\n", +" flag=1\n", +" printf('\n#***Enter the contents of the file %s*****\n# [Enter ^ in a newline to end and close the file]\n',nam)\n", +" printf('\n\n$ cat > %s #to create a file named %s and fill its contents\n',nam,nam)\n", +" fhdr=mopen(nam,'wt')\n", +" i=1\n", +" while %t\n", +" cont=input(string(i)+'. ','s')\n", +" if (cont=='^') then\n", +" break\n", +" end\n", +" mfprintf(fhdr,'%s\n',cont)\n", +" i=i+1\n", +" end\n", +" mclose(fhdr)\n", +" end\n", +"end\n", +"if flag==1 then\n", +" i=1\n", +" clc\n", +" printf('\n ===========> %s <============\n\n\n',nam)\n", +" fhdr=mopen(nam,'rt')\n", +" while %t\n", +" [n,a]=mfscanf(fhdr,'%c')\n", +" if meof(fhdr) then\n", +" break\n", +" end\n", +" printf('%c',a)\n", +" i=i+1\n", +" end\n", +" mclose(fhdr)\n", +" printf('\n\n%d characters present in the file.\n[hit ENTER to continue]\n',i)\n", +" halt('')\n", +"else\n", +" printf('\n\n# file %s is not found and not created also\n',nam)\n", +"end\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)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.2: cp_command.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"clear\n", +"mode(-1)\n", +"flag=1\n", +"pwd\n", +"xt=ans\n", +"flag=1\n", +"clc\n", +"\n", +"printf('Example 2 : Show the method of copying files in unix using the cp command \n')\n", +"disp('****************************************************************')\n", +"disp('Answer : ')\n", +"disp('INSTRUCTIONS : ')\n", +"printf('\nHere all instructions are preloaded in the form of a demo\nPRESS ENTER AFTER EACH COMMAND to see its RESULT\nPRESS 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", +"\n", +"src=input('# Enter the name of the file[or directory] which you want to copy : ','s')\n", +"if isdir(src) then\n", +"destn=input('# Enter the name of the directory which you want to copy into : ','s')\n", +"else\n", +" destn=input('# Enter the name of the file[or directory] which you want to copy into : ','s')\n", +"end\n", +"\n", +"flag=0\n", +"printf('\n $ cp %s %s \t#copies file[or directory] contents of %s to %s\n',src,destn,src,destn)\n", +"halt('')\n", +"\n", +"\n", +"if isfile(destn)&isfile(src) then\n", +" printf('cp : overwrite %s (yes/no)? ',destn)\n", +" resp=input(' ','s')\n", +" if resp=='y' then\n", +" mdelete(destn)\n", +" end\n", +"end\n", +"\n", +"if isfile(src)|isdir(src) then\n", +" flag=1\n", +" [status,msg]=copyfile(src,destn)\n", +"else\n", +" printf('\n%s : file or directory not found \n',src)\n", +" flag=0\n", +"end\n", +"\n", +"if flag==1&isfile(destn) then\n", +" i=1\n", +" printf('\n $ cat %s \t#to display the copied file %s \n\n',destn,destn)\n", +" printf('\n ===========> %s <============\n\n\n',destn)\n", +" fhdr=mopen(destn,'rt')\n", +" while %t\n", +" [n,a]=mfscanf(fhdr,'%c')\n", +" if meof(fhdr) then\n", +" break\n", +" end\n", +" printf('%c',a)\n", +" i=i+1\n", +" end\n", +" mclose(fhdr)\n", +" printf('\n\n%d characters present in the file.\n[hit ENTER to continue]\n',i)\n", +" halt('')\n", +" elseif isdir(destn)&flag==1 then\n", +" cd(destn)\n", +" mode(0)\n", +" ls\n", +" halt('Go back to previous directory ?? ')\n", +" mode(-1)\n", +" cd(xt)\n", +"else\n", +" printf('\n\n# file %s is not rewritten using copy command cp and not created also\n',destn)\n", +"end\n", +"\n", +"\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", +"\n", +"printf('\n\n\t\t\tBACK TO SCILAB CONSOLE...\nLoading initial environment')\n", +"sleep(1000)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.3: rm_command.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"clear\n", +"flag=1\n", +"mode(-1)\n", +"clc\n", +"\n", +"\n", +"printf('Example 3 : Show the method of removing files in unix using the rm command \n')\n", +"disp('****************************************************************')\n", +"disp('Answer : ')\n", +"disp('INSTRUCTIONS : ')\n", +"printf('\nHere all instructions are preloaded in the form of a demo\nPRESS ENTER AFTER EACH COMMAND to see its RESULT\nPRESS 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", +"src=input('# Enter the name of the file which you want to delete : ','s')\n", +"\n", +"flag=0\n", +"printf('\n $ rm %s \t#deletes file %s\n',src,src)\n", +"halt('')\n", +"\n", +"\n", +"if isfile(src) then\n", +" printf('rm : remove %s (yes/no)? ? ',src)\n", +" resp=input(' ','s')\n", +" if resp=='y' then\n", +" mdelete(src)\n", +" flag=1\n", +" end\n", +" else\n", +" printf('\n%s : file not found \n',src) \n", +"end\n", +"\n", +"if flag then\n", +" printf('\n $ cat %s # opening file %s to see if it exists \n',src,src)\n", +" if ~isfile(src) then\n", +" printf('\n%s : file not found \n ',src)\n", +" else\n", +" \n", +" printf('\n ===========> %s <============\n\n\n',destn)\n", +" fhdr=mopen(destn,'rt')\n", +" while %t\n", +" [n,a]=mfscanf(fhdr,'%c')\n", +" if meof(fhdr) then\n", +" break\n", +" end\n", +" printf('%c',a)\n", +" i=i+1\n", +" end\n", +" mclose(fhdr)\n", +" printf('\n\n%d characters present in the file.\n[hit ENTER to continue]\n',i)\n", +" halt('')\n", +"\n", +" end\n", +"end\n", +"\n", +"\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", +"\n", +"printf('\n\n\t\t\tBACK TO SCILAB CONSOLE...\nLoading initial environment')\n", +"sleep(1000)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.4: mv_command.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"clear\n", +"mode(-1)\n", +"flag=1\n", +"pwd\n", +"xt=ans\n", +"clc\n", +"\n", +"printf('Example 4 : Show the method of renaming files in unix using the mv command \n')\n", +"disp('****************************************************************')\n", +"disp('Answer : ')\n", +"disp('INSTRUCTIONS : ')\n", +"printf('\nHere all instructions are preloaded in the form of a demo\nPRESS ENTER AFTER EACH COMMAND to see its RESULT\nPRESS 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", +"\n", +"src=input('# Enter the name of the file[or directory] which you want to rename : ','s')\n", +"if isdir(src) then\n", +"destn=input('# Enter the new name of the directory : ','s')\n", +"else\n", +" destn=input('# Enter the name of the file[or directory] which you want to move into : ','s')\n", +"end\n", +"\n", +"flag=0\n", +"printf('\n $ mv %s %s \t#copies file[or directory] contents of %s to %s\n',src,destn,src,destn)\n", +"halt('')\n", +"\n", +"\n", +"if isfile(destn)&isfile(src) then\n", +" printf('mv : overwrite %s (yes/no)? ',destn)\n", +" resp=input(' ','s')\n", +" if resp=='y' then\n", +" mdelete(destn)\n", +" end\n", +"end\n", +"\n", +"if isfile(src)|isdir(src) then\n", +" flag=1\n", +" [status,msg]=movefile(src,destn)\n", +"else\n", +" printf('\n%s : file or directory not found \n',src)\n", +" flag=0\n", +"end\n", +"\n", +"if flag==1&isfile(destn) then\n", +" i=1\n", +" printf('\n $ cat %s \t#to display the moved file %s \n\n',destn,destn)\n", +" printf('\n ===========> %s <============\n\n\n',destn)\n", +" fhdr=mopen(destn,'rt')\n", +" while %t\n", +" [n,a]=mfscanf(fhdr,'%c')\n", +" if meof(fhdr) then\n", +" break\n", +" end\n", +" printf('%c',a)\n", +" i=i+1\n", +" end\n", +" mclose(fhdr)\n", +" printf('\n\n%d characters present in the file.\n[hit ENTER to continue]\n',i)\n", +" halt('')\n", +"elseif isdir(destn)&flag==1 then\n", +" cd(destn)\n", +" mode(0)\n", +" ls\n", +" halt('Go back to previous directory ?? ')\n", +" mode(-1)\n", +" cd(xt)\n", +"else\n", +" printf('\n # No changes done in the file \n')\n", +"end\n", +"\n", +"\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", +"\n", +"printf('\n\n\t\t\tBACK TO SCILAB CONSOLE...\nLoading initial environment')\n", +"sleep(1000)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.5: lp_command.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"mode(-1)\n", +"clear\n", +"flag=1\n", +"clc\n", +"printf('Example 5 : Show the method of file printing using the lp command \n')\n", +"disp('****************************************************************')\n", +"disp('Answer : ')\n", +"disp('INSTRUCTIONS : ')\n", +"printf('\nHere all instructions are preloaded in the form of a demo\nPRESS ENTER AFTER EACH COMMAND to see its RESULT\nPRESS 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", +"printf('\n# Enter the name of the file which you want to print \n\n')\n", +"nam=input('$ lp ','s')\n", +"printf('# This sends the file named %s to printer and gets status\n\n',nam)\n", +"if ~isfile(nam) then\n", +" flag=0\n", +" printf('\n%s : file not found \n',nam)\n", +" printf('# Create a new file named %s?\n # y : Yes \n # n : No \n',nam)\n", +" resp=input('','s')\n", +" if resp=='y' then\n", +" flag=1\n", +" printf('\n#***Enter the contents of the file %s*****\n# [Enter ^ in a newline to end and close the file]\n',nam)\n", +" printf('\n\n$ cat > %s #to create a file named %s and fill its contents\n',nam,nam)\n", +" fhdr=mopen(nam,'wt')\n", +" i=1\n", +" while %t\n", +" cont=input(string(i)+'. ','s')\n", +" if (cont=='^') then\n", +" break\n", +" end\n", +" mfprintf(fhdr,'%s\n',cont)\n", +" i=i+1\n", +" end\n", +" mclose(fhdr)\n", +" end\n", +"end\n", +"if flag then\n", +"s=toprint(nam)\n", +"if s then\n", +" printsetupbox()\n", +"else\n", +" printf('\n\nlp : printer busy \n')\n", +"end\n", +"end\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)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.6: wc_command.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"mode(-1)\n", +"clear\n", +"flag=1\n", +"clc\n", +"\n", +"printf('Example 6 : Show the method of file counting using the wc command \n')\n", +"disp('****************************************************************')\n", +"disp('Answer : ')\n", +"disp('INSTRUCTIONS : ')\n", +"printf('\nHere all instructions are preloaded in the form of a demo\nPRESS ENTER AFTER EACH COMMAND to see its RESULT\nPRESS 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", +"\n", +"\n", +"printf('\n# Enter the name of the file which you want to access \n\n')\n", +"nam=input('$ cat ','s')\n", +"printf('# This searches for a file named %s to display\n\n',nam)\n", +"\n", +"\n", +"if ~isfile(nam) then\n", +" flag=0\n", +" printf('\n%s : file not found \n',nam)\n", +" printf('# Create a new file named %s?\n # y : Yes \n # n : No \n',nam)\n", +" resp=input('','s')\n", +" if resp=='y' then\n", +" flag=1\n", +" printf('\n#***Enter the contents of the file %s*****\n# [Enter ^ in a newline to end and close the file]\n',nam)\n", +" printf('\n\n$ cat > %s #to create a file named %s and fill its contents\n',nam,nam)\n", +" fhdr=mopen(nam,'wt')\n", +" i=1\n", +" while %t\n", +" cont=input(string(i)+'. ','s')\n", +" if (cont=='^') then\n", +" break\n", +" end\n", +" mfprintf(fhdr,'%s\n',cont)\n", +" i=i+1\n", +" end\n", +" mclose(fhdr)\n", +" end\n", +"end\n", +"\n", +"if flag==1 then\n", +" c=1\n", +" w=0\n", +" l=0\n", +" clc\n", +" printf('\n $ cat %s \n',nam)\n", +" fhdr=mopen(nam,'rt')\n", +" while %t\n", +" [n,a]=mfscanf(fhdr,'%c')\n", +" if meof(fhdr) then\n", +" break\n", +" end\n", +" \n", +" printf('%c',a)\n", +" c=c+1\n", +" if ascii(a)==32 then\n", +" w=w+1\n", +" end\n", +" if ascii(a)==10 then\n", +" w=w+1\n", +" l=l+1\n", +" end\n", +" end\n", +" mclose(fhdr)\n", +" halt('')\n", +" printf('\n\n$ wc %s #to get the count in file named %s \n',nam,nam)\n", +" halt('') \n", +" printf('\t%d\t%d\t%d %s\n',l,w,c,nam)\n", +" printf('\n# This means there are %d words,%d characters\n \t and %d lines in the file %s \n',w,c,l,nam)\n", +" printf('\n\n$ wc -l %s #to get the line count in file named %s \n',nam,nam)\n", +" halt('') \n", +" printf('\t%d %s\n',l,nam)\n", +" printf('\n# Number of lines \n')\n", +" printf('\n\n$ wc -w %s #to get the word count in file named %s \n',nam,nam)\n", +" halt('') \n", +" printf('\t%d %s\n',w,nam)\n", +" printf('\n# Number of words \n')\n", +" printf('\n\n$ wc -c %s #to get the character count in file named %s \n',nam,nam)\n", +" halt('') \n", +" printf('\t%d %s\n',c,nam)\n", +" printf('\n# Number of characters \n')\n", +"end\n", +"\n", +"\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", +"\n", +"printf('\n\n\t\t\tBACK TO SCILAB CONSOLE...\nLoading initial environment')\n", +"sleep(1000)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.7: od_command.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"clear\n", +"flag=1\n", +"clc\n", +"mode(-1)\n", +"\n", +"printf('Example 7 : Show the method of file handling using the od command \n')\n", +"disp('****************************************************************')\n", +"disp('Answer : ')\n", +"disp('INSTRUCTIONS : ')\n", +"printf('\nHere all instructions are preloaded in the form of a demo\nPRESS ENTER AFTER EACH COMMAND to see its RESULT\nPRESS 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", +"\n", +"\n", +"printf('\n# Enter the name of the file which you want to access \n\n')\n", +"nam=input('$ od ','s')\n", +"printf('# This searches for a file named %s \n\n',nam)\n", +"\n", +"\n", +"if ~isfile(nam) then\n", +" flag=0\n", +" printf('\n%s : file not found \n',nam)\n", +" printf('# Create a new file named %s?\n # y : Yes \n # n : No \n',nam)\n", +" resp=input('','s')\n", +" if resp=='y' then\n", +" flag=1\n", +" printf('\n#***Enter the contents of the file %s*****\n# [Enter ^ in a newline to end and close the file]\n',nam)\n", +" printf('\n\n$ cat > %s #to create a file named %s and fill its contents\n',nam,nam)\n", +" fhdr=mopen(nam,'wt')\n", +" i=1\n", +" while %t\n", +" cont=input(string(i)+'. ','s')\n", +" if (cont=='^') then\n", +" break\n", +" end\n", +" mfprintf(fhdr,'%s\n',cont)\n", +" i=i+1\n", +" end\n", +" mclose(fhdr)\n", +" end\n", +"end\n", +"\n", +"if flag==1 then\n", +" i=1\n", +" clc\n", +" printf('\n $ od %s #to display %s in octal characters\n\n',nam,nam)\n", +" printf('\n ===========> %s <============\n\n\n',nam)\n", +" fhdr=mopen(nam,'rt')\n", +" while %t\n", +" [n,a]=mfscanf(fhdr,'%c')\n", +" if meof(fhdr) then\n", +" break\n", +" end\n", +" printf(' %o',ascii(a))\n", +" if ascii(a)==10 then\n", +" printf('\n')\n", +" end\n", +" i=i+1\n", +" end\n", +" mclose(fhdr)\n", +" printf('\n\n%d characters present in the file.\n[hit ENTER to continue]\n',i)\n", +" halt('')\n", +"else\n", +" printf('\n\n# file %s is not found and not created also\n',nam)\n", +"end\n", +"\n", +"flag=flag+1\n", +"\n", +"octs=blanks(0)\n", +"if flag==2 then\n", +" i=1\n", +" clc\n", +" printf('\n $ od -bc %s #to display %s in octal characters\n\n',nam,nam)\n", +" printf('\n ===========> %s <============\n\n\n',nam)\n", +" fhdr=mopen(nam,'rt')\n", +" while %t\n", +" [n,a]=mfscanf(fhdr,'%c')\n", +" if meof(fhdr) then\n", +" break\n", +" end\n", +" printf(' %c ',a)\n", +" octs=octs+string(dec2oct(ascii(a)))+' '\n", +" if ascii(a)==10 then\n", +" printf('%s\n\n',octs)\n", +" clear('octs')\n", +" octs=blanks(0)\n", +" end\n", +" i=i+1\n", +" end\n", +" mclose(fhdr)\n", +" printf('\n\n%d characters present in the file.\n[hit ENTER to continue]\n',i)\n", +" halt('')\n", +"else\n", +" printf('\n\n# file %s is not found and not created also\n',nam)\n", +"end\n", +"\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", +"\n", +"printf('\n\n\t\t\tBACK TO SCILAB CONSOLE...\nLoading initial environment')\n", +"sleep(1000)" + ] + } +, +{ + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5.8: cmp_command.sce" + ] + }, + { +"cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], +"source": [ +"clear\n", +"flag=1\n", +"clc\n", +"mode(-1)\n", +"\n", +"printf('Example 8 : Show the method of comparing files using cmp command \n')\n", +"disp('****************************************************************')\n", +"disp('Answer : ')\n", +"disp('INSTRUCTIONS : ')\n", +"printf('\nHere all instructions are preloaded in the form of a demo\nPRESS ENTER AFTER EACH COMMAND to see its RESULT\nPRESS 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", +"\n", +"\n", +"printf('\n# Enter the name of the two files which you want to compare \n')\n", +"fil(1)=input('','s')\n", +"fil(2)=input('','s')\n", +"printf('\n\n $ cmp %s %s #to compare the files %s and %s\n',fil(1),fil(2),fil(1),fil(2))\n", +"\n", +"for i=1:2\n", +"if ~isfile(fil(i)) then\n", +" flag(i)=0\n", +" printf('\n%s : file not found \n',fil(i))\n", +" printf('# Create a new file named %s?\n # y : Yes \n # n : No \n',fil(i))\n", +" resp=input('','s')\n", +" if resp=='y' then\n", +" flag(i)=1\n", +" printf('\n#***Enter the contents of the file %s*****\n# [Enter ^ in a newline to end and close the file]\n',fil(i))\n", +" printf('\n\n$ cat > %s #to create a file named %s and fill its contents\n',fil(i),fil(i))\n", +" fhdr=mopen(fil(i),'wt')\n", +" count=1\n", +" while %t\n", +" cont=input(string(count)+'. ','s')\n", +" if (cont=='^') then\n", +" break\n", +" end\n", +" mfprintf(fhdr,'%s\n',cont)\n", +" count=count+1\n", +" end\n", +" mclose(fhdr)\n", +" end\n", +"end\n", +"end\n", +"\n", +"\n", +"if flag(1)&flag(2) then\n", +" clc\n", +" printf('\n $ cmp %s %s #to compare files %s and %s \n',fil(1),fil(2),fil(1),fil(2))\n", +" fhdr1=mopen(fil(1),'rt')\n", +" fhdr2=mopen(fil(2),'rt')\n", +" l=0\n", +" cr=1\n", +" while %t\n", +" [n,a1]=mfscanf(fhdr1,'%c')\n", +" [n,a2]=mfscanf(fhdr2,'%c')\n", +" if meof(fhdr1)&meof(fhdr2) then\n", +" printf(' \n# No output means both the files are identical \n')\n", +" break\n", +" elseif (meof(fhdr1)&~meof(fhdr2))|(meof(fhdr2)&~meof(fhdr1))|a1~=a2\n", +" printf(' %s %s differ : char %d , line %d \n',fil(1),fil(2),cr,l+1)\n", +" printf(' # This shows that %dth character in %dth line do not match\n\n',cr,l+1)\n", +" break\n", +" end\n", +" cr=cr+1\n", +" if ascii(a1)==10 then\n", +" l=l+1\n", +" cr=1\n", +" end\n", +" end\n", +" mclose(fhdr1)\n", +" mclose(fhdr2)\n", +" halt('')\n", +"else\n", +" printf('\n\n# file %s or %s is not found \n',fil(1),fil(2))\n", +"end\n", +"\n", +"\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", +"\n", +"printf('\n\n\t\t\tBACK TO SCILAB CONSOLE...\nLoading initial environment')\n", +"sleep(1000)" + ] + } +], +"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 +} |