{"id":6731,"date":"2016-01-10T21:17:03","date_gmt":"2016-01-10T20:17:03","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/"},"modified":"2016-01-10T21:17:03","modified_gmt":"2016-01-10T20:17:03","slug":"linux-how-to-check-the-exit-status-of-several-piped-commands","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/","title":{"rendered":"Linux &#8211; How to check the exit status of several piped commands"},"content":{"rendered":"<p>As piping in bash commands is common and very usefull, controling the exit status of each piped commands in bash scripting can be vital, especially for backups.<\/p>\n<div>\n<div>\n<div>\n<div>\n<div>I was checking by a customer the backups of a critical MySQL instance and was surprised even stunned that the return status of all of them was always\u00a0 successfull when tested but physically on disk, the dumps were all empty.<\/div>\n<div>No valid backups since a long time meaning no possible recovery.<\/div>\n<div>Oups! How can it be?<\/div>\n<\/div>\n<div>I immediately opened the backup script with &#8220;vi&#8221; and had a look to the used statement which was the following:<\/div>\n<div>\n<pre class=\"brush: shell; gutter: true; first-line: 1\">mysqldump -u $USERNAME -p $PASS $DBNAME | bzip2 -9c -c &gt; Dump.sql.gz<\/pre>\n<pre class=\"commands-codes\">Now, what if the backup failed and the bzip2 command succeeded?\nIn fact, the exit status will be the one of the last command.<\/pre>\n<pre class=\"brush: shell; gutter: true; first-line: 1\">echo $? \n0<\/pre>\n<p class=\"commands-codes\">And this will be always successfull.<\/p>\n<\/div>\n<div>So, the\u00a0 solution to check the exit status of a particular command in piped commands is to use an inbuilt linux variable called\u00a0 PIPESTATUS.<\/div>\n<div>PIPESTATUS is an array variable which contain the exit status of every piped commands.<\/div>\n<div>In our case,<\/div>\n<div>echo ${PIPESTATUS[0]} will refer to the backup\u00a0 and will be greater than 0 if it fails<br \/>\necho ${PIPESTATUS[1]} will refer to the compression<\/div>\n<div>echo ${PIPESTATUS[*]} or echo ${PIPESTATUS[@]} will give you the status of both.<\/div>\n<div>\n<div>\n<pre class=\"commands-codes\">So, one solution in our example could be:<\/pre>\n<pre class=\"brush: shell; gutter: true; first-line: 1\">mysqldump -u $USERNAME -p $PASS $DBNAME | bzip2 -9c -c &gt; Dump.sql.gz\n\nif [ ${PIPESTATUS[0]} -ne \"0\" ]\nthen\n    echo \"the MySQL Database backup failed with Error: ${PIPESTATUS[0]}\";\nelse\n    echo \"The MySQL Database backup was successfull!\";\nfi<\/pre>\n<pre class=\"commands-codes\">I hope it will help...<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>As piping in bash commands is common and very usefull, controling the exit status of each piped commands in bash scripting can be vital, especially for backups. I was checking by a customer the backups of a critical MySQL instance and was surprised even stunned that the return status of all of them was always\u00a0 [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":6732,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[42],"tags":[46],"type_dbi":[],"class_list":["post-6731","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-operating-systems","tag-linux-unix"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Linux - How to check the exit status of several piped commands - dbi Blog<\/title>\n<meta name=\"description\" content=\"Blog about Linux command: PIPESTATUS\" \/>\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.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux - How to check the exit status of several piped commands\" \/>\n<meta property=\"og:description\" content=\"Blog about Linux command: PIPESTATUS\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2016-01-10T20:17:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/AP-MySql-Architecture.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1518\" \/>\n\t<meta property=\"og:image:height\" content=\"1058\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Open source Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Open source Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/\"},\"author\":{\"name\":\"Open source Team\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/59554f0d99383431eb6ed427e338952b\"},\"headline\":\"Linux &#8211; How to check the exit status of several piped commands\",\"datePublished\":\"2016-01-10T20:17:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/\"},\"wordCount\":196,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/AP-MySql-Architecture.jpg\",\"keywords\":[\"Linux\/UNIX\"],\"articleSection\":[\"Operating systems\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/\",\"name\":\"Linux - How to check the exit status of several piped commands - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/AP-MySql-Architecture.jpg\",\"datePublished\":\"2016-01-10T20:17:03+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/59554f0d99383431eb6ed427e338952b\"},\"description\":\"Blog about Linux command: PIPESTATUS\",\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#primaryimage\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/AP-MySql-Architecture.jpg\",\"contentUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/AP-MySql-Architecture.jpg\",\"width\":1518,\"height\":1058},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux &#8211; How to check the exit status of several piped commands\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/59554f0d99383431eb6ed427e338952b\",\"name\":\"Open source Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"caption\":\"Open source Team\"},\"url\":\"https:\/\/www.dbi-services.com\/blog\/author\/open-source-team\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Linux - How to check the exit status of several piped commands - dbi Blog","description":"Blog about Linux command: PIPESTATUS","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.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/","og_locale":"en_US","og_type":"article","og_title":"Linux - How to check the exit status of several piped commands","og_description":"Blog about Linux command: PIPESTATUS","og_url":"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/","og_site_name":"dbi Blog","article_published_time":"2016-01-10T20:17:03+00:00","og_image":[{"width":1518,"height":1058,"url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/AP-MySql-Architecture.jpg","type":"image\/jpeg"}],"author":"Open source Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Open source Team","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/"},"author":{"name":"Open source Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/59554f0d99383431eb6ed427e338952b"},"headline":"Linux &#8211; How to check the exit status of several piped commands","datePublished":"2016-01-10T20:17:03+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/"},"wordCount":196,"commentCount":0,"image":{"@id":"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/AP-MySql-Architecture.jpg","keywords":["Linux\/UNIX"],"articleSection":["Operating systems"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/","url":"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/","name":"Linux - How to check the exit status of several piped commands - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#primaryimage"},"image":{"@id":"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/AP-MySql-Architecture.jpg","datePublished":"2016-01-10T20:17:03+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/59554f0d99383431eb6ed427e338952b"},"description":"Blog about Linux command: PIPESTATUS","breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#primaryimage","url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/AP-MySql-Architecture.jpg","contentUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/AP-MySql-Architecture.jpg","width":1518,"height":1058},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/linux-how-to-check-the-exit-status-of-several-piped-commands\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Linux &#8211; How to check the exit status of several piped commands"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/59554f0d99383431eb6ed427e338952b","name":"Open source Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","caption":"Open source Team"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/open-source-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/6731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=6731"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/6731\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media\/6732"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=6731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=6731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=6731"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=6731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}