{"id":77772,"date":"2020-01-22T16:43:12","date_gmt":"2020-01-22T11:13:12","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=77772"},"modified":"2024-04-12T14:29:55","modified_gmt":"2024-04-12T08:59:55","slug":"datatypes-and-variables","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/","title":{"rendered":"Datatypes and Variables"},"content":{"rendered":"<h4 class=\"w-post-elm post_title us_custom_49f317a8 align_center entry-title color_link_inherit\" style=\"text-align: left;\"><strong>Datatypes and Variables<\/strong><\/h4>\n<p>NASL allows for the assignment of values to variables that can be manipulated by a NASL script. Unlike a strongly typed language such as C, NASL does not require you to predefine a variable&#8217;s type. In NASL, the variable type is determined automatically when a variable is assigned a specific value. NASL recognizes two valid datatypes: scalars and arrays. A scalar can be a number or a string, while an array is a collection of scalars.<\/p>\n<p><strong>Numbers: <\/strong>NASL allows variables to hold integer values\u2014for example, the number 11. It is also possible to assign numeric values to variables using a hexadecimal representation. You write hexadecimal numbers in NASL using a leading &#8220;0x&#8221; prefix. For example, the hexadecimal number 0x1b holds the value 27 when represented as an integer in base-10 notation. Type the following script into a file:<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p>h=0x1b;<\/p>\n<p>display (&#8220;The value of h is &#8220;,h,&#8221;\\n&#8221;);<\/p>\n<p>Now run it using the NASL interpreter to see the output:<\/p>\n[notroot]$ nasl hex.nasl<\/p>\n<p>The value of h is 27<\/p>\n<p>It is also possible to input numerical values in octal notation form, which uses base- 8 notation by placing a leading &#8220;0&#8221; prefix. For example, the x and y are equivalent in the following example:<\/p>\n<p>x=014; #octal<\/p>\n<p>y=12; #decimal<\/p>\n<p><strong>Strings: <\/strong>A string is a collection of characters. abcdefg, Hello World, and Boeing 747 are all examples of strings. Consider the following NASL script:<\/p>\n<p>mystring=&#8221;Hello. I am a string!\\n&#8221;;<\/p>\n<p>display(mystring);<\/p>\n<p>The \\n at the end of mystring is an escape character and is equivalent to a newline character. Table 1-1 lists common escape characters applicable to NASL.<\/p>\n<p><strong>Table 1-1. Escape characters<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-69376\" src=\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/08\/image013.png\" alt=\"image013\" width=\"484\" height=\"545\" \/><\/p>\n<p>Note that a string inside double quotes (&#8220;) is left as is. Therefore, if you define a string using double quotes, escape sequences will not be translated. Also note that the display( ) function calls the string( ) function before displaying data on the console, and it is the string( ) function that converts the escape sequences. That is why our escape sequences are translated in the preceding examples even though we define them using double quotes.<\/p>\n<p><strong>Arrays and Hashes: <\/strong>An array is a collection of numbers or strings that can be indexed using a numeric subscript. Consider the following NASL script:<\/p>\n<p>myarray=make_list(1,&#8221;two&#8221;);<\/p>\n<p>display(&#8220;The value of the first item is &#8220;,myarray[0],&#8221; \\n&#8221;);<\/p>\n<p>display(&#8220;The value of the second item is &#8220;,myarray[1],&#8221; \\n&#8221;);<\/p>\n<p>The script displays the following when executed:<\/p>\n<p>The value of the first item is 1<\/p>\n<p>The value of the second item is two<\/p>\n<p>Notice that the array subscripts begin at 0, and that is why the first element is obtained using the [0] subscript.<\/p>\n<p>Like arrays, hashes are also collections of numbers or strings. However, elements in hashes have a key value associated with them that can be used to obtain the element. You can use the make_array( ) function call to define a hash. Because every element must have an associated key value, the function call requires an even number of arguments. The following is a definition of a hash that contains port numbers for the Telnet protocol (port 23) and HTTP (port 80):<\/p>\n<p>myports=make_array(&#8216;telnet&#8217;,23,&#8217;http&#8217;,80);<\/p>\n<p>Now, myports[&#8216;telnet&#8217;] gives you the value of 23, while myports[&#8216;http&#8217;] evaluates to 80.<\/p>\n<p><strong>Local and Global Variables<\/strong><\/p>\n<p>Variables exist only within the blocks in which they are defined. A block is a collection of statements enclosed by special statements such as loops and function calls. For example, if you define a variable within a particular function call, it will not exist when the function call returns. At times, it is necessary to define variables that should exist globally; in such cases you should use global_var to define them:<\/p>\n<p>global_var myglobalvariable;<\/p>\n<p>Variables are local by default. You can also use local_var to state this explicitly.<\/p>\n<h3>Apply for Network Security Open Source Software Developer Certification Now!!<\/h3>\n<p><a href=\"https:\/\/www.vskills.in\/certification\/network-security-open-source-software-developer-certification\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.vskills.in\/certification\/network-security-open-source-software-developer-certification<\/a><\/p>\n<h4><strong><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/network-security-open-source-software-certification\/\" target=\"_blank\" rel=\"noreferrer noopener\">Back to Tutorial<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>Datatypes and Variables NASL allows for the assignment of values to variables that can be manipulated by a NASL script. Unlike a strongly typed language such as C, NASL does not require you to predefine a variable&#8217;s type. In NASL, the variable type is determined automatically when a variable is assigned a specific value. NASL&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[9066],"tags":[9072],"class_list":["post-77772","page","type-page","status-publish","hentry","category-network-security-open-source","tag-datatypes-and-variables"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Datatypes and Variables - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Datatypes and Variables - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Datatypes and Variables NASL allows for the assignment of values to variables that can be manipulated by a NASL script. Unlike a strongly typed language such as C, NASL does not require you to predefine a variable&#8217;s type. In NASL, the variable type is determined automatically when a variable is assigned a specific value. NASL...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:59:55+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/08\/image013.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/\",\"name\":\"Datatypes and Variables - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/08\/image013.png\",\"datePublished\":\"2020-01-22T11:13:12+00:00\",\"dateModified\":\"2024-04-12T08:59:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/#primaryimage\",\"url\":\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/08\/image013.png\",\"contentUrl\":\"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/08\/image013.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Datatypes and Variables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Datatypes and Variables - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/","og_locale":"en_US","og_type":"article","og_title":"Datatypes and Variables - Tutorial","og_description":"Datatypes and Variables NASL allows for the assignment of values to variables that can be manipulated by a NASL script. Unlike a strongly typed language such as C, NASL does not require you to predefine a variable&#8217;s type. In NASL, the variable type is determined automatically when a variable is assigned a specific value. NASL...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:59:55+00:00","og_image":[{"url":"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/08\/image013.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/","name":"Datatypes and Variables - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/#primaryimage"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/08\/image013.png","datePublished":"2020-01-22T11:13:12+00:00","dateModified":"2024-04-12T08:59:55+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/#primaryimage","url":"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/08\/image013.png","contentUrl":"http:\/\/www.vskills.in\/lms\/wp-content\/uploads\/2016\/08\/image013.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/datatypes-and-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Datatypes and Variables"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/77772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=77772"}],"version-history":[{"count":5,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/77772\/revisions"}],"predecessor-version":[{"id":134452,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/77772\/revisions\/134452"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=77772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=77772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=77772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}