Skip to main content
Code Review

Return to Answer

Fix printf format strings
Source Link
tripleee
  • 531
  • 7
  • 19
#!/bin/bash
set -e
run () {
 local task=1ドル
 local desc=2ドル
 shift 2
 #local rc
 # More portable would be to use tput instead of literal escape codes
 # Avoid uppercase for non-system variables
 local red="033円[1;31m"
 local green="033円[1;32m"
 local yellow="033円[1;33m"
 local nocolor="033円[0m"
 printf "${green}Step"%sStep $task%s: $desc%s.${nocolor}\n"%s\n" "$green" "$task" "$desc" "$nocolor"
 printf "${yellow}$@${nocolor}\n""%ss%s\n" "$yellow" "$*" "$nocolor"
 if sudo "$@"; then
 printf "${green}Success"%sSuccess.${nocolor}\n"%s\n" "$green" "$nocolor"
 else
 # The fix to capture the failed command's exit code
 # was removed by the OP in an edit of the code
 # but I'm recording it here for posterity.
 #rc=$?
 #printf "${red}Failure"%sFailure: $task${nocolor}\n"%s%s\n" "$red" "$task" "$nocolor"
 #return $rc
 printf "${red}Failure"%sFailure: $task${nocolor}\n"%s%s\n" "$red" "$task" "$nocolor"
 return $task
 fi
}
while IFS=: read exit cmd doco; do
 run $exit "$doco" $cmd || exit
done <<____HERE
 1:dpkg --configure -a :configure packages
 2:apt-get install --fix-broken :fix broken dependencies
 3:apt-get update :update cache
 4:apt-get upgrade :upgrade packages
 5:apt-get dist-upgrade :upgrade distribution
 6:apt-get --purge autoremove :remove unused packages
 7:apt-get autoclean :clean up
____HERE
#!/bin/bash
set -e
run () {
 local task=1ドル
 local desc=2ドル
 shift 2
 #local rc
 # More portable would be to use tput instead of literal escape codes
 # Avoid uppercase for non-system variables
 local red="033円[1;31m"
 local green="033円[1;32m"
 local yellow="033円[1;33m"
 local nocolor="033円[0m"
 printf "${green}Step $task: $desc.${nocolor}\n"
 printf "${yellow}$@${nocolor}\n"
 if sudo "$@"; then
 printf "${green}Success.${nocolor}\n"
 else
 # The fix to capture the failed command's exit code
 # was removed by the OP in an edit of the code
 # but I'm recording it here for posterity.
 #rc=$?
 #printf "${red}Failure: $task${nocolor}\n"
 #return $rc
 printf "${red}Failure: $task${nocolor}\n"
 return $task
 fi
}
while IFS=: read exit cmd doco; do
 run $exit "$doco" $cmd || exit
done <<____HERE
 1:dpkg --configure -a :configure packages
 2:apt-get install --fix-broken :fix broken dependencies
 3:apt-get update :update cache
 4:apt-get upgrade :upgrade packages
 5:apt-get dist-upgrade :upgrade distribution
 6:apt-get --purge autoremove :remove unused packages
 7:apt-get autoclean :clean up
____HERE
#!/bin/bash
set -e
run () {
 local task=1ドル
 local desc=2ドル
 shift 2
 #local rc
 # More portable would be to use tput instead of literal escape codes
 # Avoid uppercase for non-system variables
 local red="033円[1;31m"
 local green="033円[1;32m"
 local yellow="033円[1;33m"
 local nocolor="033円[0m"
 printf "%sStep %s: %s.%s\n" "$green" "$task" "$desc" "$nocolor"
 printf "%ss%s\n" "$yellow" "$*" "$nocolor"
 if sudo "$@"; then
 printf "%sSuccess.%s\n" "$green" "$nocolor"
 else
 # The fix to capture the failed command's exit code
 # was removed by the OP in an edit of the code
 # but I'm recording it here for posterity.
 #rc=$?
 #printf "%sFailure: %s%s\n" "$red" "$task" "$nocolor"
 #return $rc
 printf "%sFailure: %s%s\n" "$red" "$task" "$nocolor"
 return $task
 fi
}
while IFS=: read exit cmd doco; do
 run $exit "$doco" $cmd || exit
done <<____HERE
 1:dpkg --configure -a :configure packages
 2:apt-get install --fix-broken :fix broken dependencies
 3:apt-get update :update cache
 4:apt-get upgrade :upgrade packages
 5:apt-get dist-upgrade :upgrade distribution
 6:apt-get --purge autoremove :remove unused packages
 7:apt-get autoclean :clean up
____HERE
$desc not $doco inside the function
Source Link
tripleee
  • 531
  • 7
  • 19
#!/bin/bash
set -e
run () {
 local task=1ドル
 local desc=2ドル
 shift 2
 #local rc
 # More portable would be to use tput instead of literal escape codes
 # Avoid uppercase for non-system variables
 local red="033円[1;31m"
 local green="033円[1;32m"
 local yellow="033円[1;33m"
 local nocolor="033円[0m"
 printf "${green}Step $task: $doco$desc.${nocolor}\n"
 printf "${yellow}$@${nocolor}\n"
 if sudo "$@"; then
 printf "${green}Success.${nocolor}\n"
 else
 # The fix to capture the failed command's exit code
 # was removed by the OP in an edit of the code
 # but I'm recording it here for posterity.
 #rc=$?
 #printf "${red}Failure: $task${nocolor}\n"
 #return $rc
 printf "${red}Failure: $task${nocolor}\n"
 return $task
 fi
}
while IFS=: read exit cmd doco; do
 run $exit "$doco" $cmd || exit
done <<____HERE
 1:dpkg --configure -a :configure packages
 2:apt-get install --fix-broken :fix broken dependencies
 3:apt-get update :update cache
 4:apt-get upgrade :upgrade packages
 5:apt-get dist-upgrade :upgrade distribution
 6:apt-get --purge autoremove :remove unused packages
 7:apt-get autoclean :clean up
____HERE
#!/bin/bash
set -e
run () {
 local task=1ドル
 local desc=2ドル
 shift 2
 #local rc
 # More portable would be to use tput instead of literal escape codes
 # Avoid uppercase for non-system variables
 local red="033円[1;31m"
 local green="033円[1;32m"
 local yellow="033円[1;33m"
 local nocolor="033円[0m"
 printf "${green}Step $task: $doco.${nocolor}\n"
 printf "${yellow}$@${nocolor}\n"
 if sudo "$@"; then
 printf "${green}Success.${nocolor}\n"
 else
 # The fix to capture the failed command's exit code
 # was removed by the OP in an edit of the code
 # but I'm recording it here for posterity.
 #rc=$?
 #printf "${red}Failure: $task${nocolor}\n"
 #return $rc
 printf "${red}Failure: $task${nocolor}\n"
 return $task
 fi
}
while IFS=: read exit cmd doco; do
 run $exit "$doco" $cmd || exit
done <<____HERE
 1:dpkg --configure -a :configure packages
 2:apt-get install --fix-broken :fix broken dependencies
 3:apt-get update :update cache
 4:apt-get upgrade :upgrade packages
 5:apt-get dist-upgrade :upgrade distribution
 6:apt-get --purge autoremove :remove unused packages
 7:apt-get autoclean :clean up
____HERE
#!/bin/bash
set -e
run () {
 local task=1ドル
 local desc=2ドル
 shift 2
 #local rc
 # More portable would be to use tput instead of literal escape codes
 # Avoid uppercase for non-system variables
 local red="033円[1;31m"
 local green="033円[1;32m"
 local yellow="033円[1;33m"
 local nocolor="033円[0m"
 printf "${green}Step $task: $desc.${nocolor}\n"
 printf "${yellow}$@${nocolor}\n"
 if sudo "$@"; then
 printf "${green}Success.${nocolor}\n"
 else
 # The fix to capture the failed command's exit code
 # was removed by the OP in an edit of the code
 # but I'm recording it here for posterity.
 #rc=$?
 #printf "${red}Failure: $task${nocolor}\n"
 #return $rc
 printf "${red}Failure: $task${nocolor}\n"
 return $task
 fi
}
while IFS=: read exit cmd doco; do
 run $exit "$doco" $cmd || exit
done <<____HERE
 1:dpkg --configure -a :configure packages
 2:apt-get install --fix-broken :fix broken dependencies
 3:apt-get update :update cache
 4:apt-get upgrade :upgrade packages
 5:apt-get dist-upgrade :upgrade distribution
 6:apt-get --purge autoremove :remove unused packages
 7:apt-get autoclean :clean up
____HERE
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link
Link to lowercase variable names
Source Link
tripleee
  • 531
  • 7
  • 19
Loading
Link to printf recommendation
Source Link
tripleee
  • 531
  • 7
  • 19
Loading
$task and $rc the author switched in Failure section, obviously and ____HERE had one _ more
Source Link
Loading
Source Link
tripleee
  • 531
  • 7
  • 19
Loading
lang-bash

AltStyle によって変換されたページ (->オリジナル) /