0

I have generated multiple statements via for loop those statements has to be executed and store each result in a variable using shell script

for example :

ls
ls -l /abc
ls -l /def

Output : 1 is success and 0 is failure

Command1:0
Command2:1
Command3:0
oguz ismail
  • 1
  • 16
  • 47
  • 69
user1485267
  • 1,295
  • 2
  • 10
  • 19
  • ...wait, you want the *exit status*, not the *result*? Where do these commands come from? Are they in an array? Are they in subsequent lines of a script? – Charles Duffy Jun 11 '19 at 18:54
  • you can check a general answer here https://stackoverflow.com/questions/55927187/function-to-check-the-return-code-of-supplied-command – P.... Jun 11 '19 at 18:54
  • `store_result() { declare -g -a results; local retval; "$@"; retval=$?; results+=( "$retval" ); return "$retval"; }` -- thereafter, `store_result ls; store_result ls -l /abc; store_result ls -l /def` and your `results` array will have the desired values. – Charles Duffy Jun 11 '19 at 18:56
  • Is there anyway which we can add via simple awk statement – user1485267 Jun 12 '19 at 14:11
  • I have multiple commands which executes one after another like two statements are running independently like for example result1 : abc result2:0 result3:def result4:1 then expected output has to be abc : 0 and in next line def : 1 – user1485267 Jun 12 '19 at 14:11

0 Answers0