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
5
  • ...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? Commented Jun 11, 2019 at 18:54
  • you can check a general answer here stackoverflow.com/questions/55927187/… Commented Jun 11, 2019 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. Commented Jun 11, 2019 at 18:56
  • Is there anyway which we can add via simple awk statement Commented Jun 12, 2019 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 Commented Jun 12, 2019 at 14:11

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.