Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

FORT BRAGG, N.C. — A military judge spared Army Sgt. Bowe Bergdahl from prison for walking off his post in Afghanistan in 2009, sentencing him on Friday to a dishonorable discharge and a reduction in rank but no time behind bars.

"Sgt. Bergdahl has looked forward to today for a long time," one of his defense attorneys, Eugene Fidell, told reporters after the sentencing.

The decision drew swift rebuke from President Donald Trump, who tweeted that it was "a complete and total disgrace to our Country and to our Military."

Bergdahl, who pleaded guilty to desertion and misbehavior before the enemy, faced up to life in prison. Defense attorneys had asked for leniency, citing the five years Bergdahl spent in Taliban captivity, criticism from Trump on the campaign trail, and his mental state.

But prosecutors hoped to prove that Bergdahl endangered his comrades by abandoning his post and called for a 14-year prison sentence.

Related: Prosecutors in Bowe Bergdahl Case Call for 14-Year Prison Sentence

The judge, Col. Jeffery Nance, reduced Bergdahl's rank to private and said he had to forfeit pay equal to $1,000 per month for 10 months. Nance did not make any other comments.

Bergdahl appeared nervous, clenching his jaw as he waited for the judge to enter the courtroom. Two women on his defense team patted his back.

Reading from a statement outside the courthouse, Fidell said Bergdahl was grateful to those who had searched for him and worked to secure his release, plus "those good-hearted people who have expressed sympathy for his plight and have been willing to wait for the facts to emerge rather than relying on assumptions or politically inspired misinformation."

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

"On the other hand, President Trump's unprincipled effort to stoke a lynch-mob atmosphere while seeking our nation's highest office has cast a dark cloud over the case," he continued, referring to criticism Trump made about Bergdahl in the past.

Dishonorable discharges automatically trigger an appeal in military court. Fidell said he would pursue an appeal, citing a need for Bergdahl to receive benefits in light of his mental health.

"A dishonorable discharge is a lifetime stigma," Fidell said.

He added that he looked forward to the appeal and that he felt Trump weighing in on the case had caused one of the "most preposterous" legal minefields in American history.

"We think there's an extremely strong basis for dismissal of the case," Fidell said.

The ruling capped a sentencing hearing where emotions at times ran high, with soldiers, relatives of soldiers, experts, a psychiatrist and Bergdahl himself testifying.

In one of the most powerful testimonies, the wife of a soldier who was shot in the head while searching for Bergdahl spoke of how their family's lives were forever changed.

"He's lost me as a wife, essentially, because instead of being his wife, I'm his caregiver," said Shannon Allen, whose husband, Master Sgt. Mark Allen, is wheelchair-bound and cannot speak. "I mean, we can't hold hands anymore, unless I pry open his hand and place mine in it."

Later that day, Bergdahl unexpectedly took the stand. Breaking down, he called abandoning his post a "terrible move" and detailed several unsuccessful escape attempts from his captors. He recalled painful bed sores and said his captors burned the bottoms of his feet. He described having uncontrollable diarrhea, and open sores on his ankles and his head where a blindfold rubbed against his skin.

Bergdahl walked off from his base in Afghanistan on June 30, 2009, and was captured by the Taliban within hours. He was then released in 2014 in a prisoner swap arranged by President Barack Obama. The deal was criticized by Trump and other Republicans.

While running for president, Trump disparaged Bergdahl, 31, as a "dirty rotten traitor" and called for him to be executed by firing squad or tossed out of a plane without a parachute.

Nance temporarily halted the sentencing hearing after seeing a video of Trump on Oct. 16 referring to his campaign trail comments in which he said he couldn't talk about Bergdahl, but added, "But I think people have heard my comments in the past." Nance later denied a motion by the defense to dismiss the case based on Trump's comments.

Bergdahl has said he left his post to reach a commander at another post to raise concern about problems in his unit. Defense attorney Capt. Nina Banks argued the torture he endured from his captors was punishment enough, followed by being labeled a traitor and a deserter by Trump when he came home.

“Sgt. Bergdahl has been punished enough,” she said. “It is undisputed Sgt. Bergdahl paid a bitter price.”

She also pointed to his mental disorders. A psychiatrist testified that Bergdahl was suffering from schizotypal personality disorder, a schizophrenia-like condition, and post-traumatic stress disorder when he walked away in Afghanistan. Deserting his post was consistent with Bergdahl's personality disorder, said Dr. Charles Morgan.

"I think he believes there are times that, if it's the morally right thing to do, you have to break the rules," he said. "There's not a thinking through of: 'Are there other ways to achieve this goal?'"

Shauna Williams reported from Fort Bragg, North Carolina, and Elizabeth Chuck from New York.