
저는 bash 스크립팅이 처음이라 어리석은 질문을 하면 죄송합니다 xD
매일 cli 명령을 실행하는 스크립트를 만들고 있습니다. cli 명령에서 얻은 출력은 다음 날 사용해야 하는 ID입니다. 그래서 그것은 같다
cli-command-delete $oldid; # here we delete the old id which was generated past day
newid=$(cli-command-create) #here we get the new id.
이제 저장하고 싶어요새로운 로고에서 기존 로고로, 다음 날 또는 다음에 스크립트가 실행될 때 사용됩니다. 환경 변수로 저장하고 새 ID를 만든 후 값을 바꾸려면 어떻게 해야 합니까? 가상머신을 다시 시작하면 이 값이 저장되나요? 내보내기를 사용하는 것을 Google에서 봤는데 다른 이름으로 저장하는 방법이 궁금합니다.
답변1
"가상머신을 다시 시작하면 이 값이 저장되나요?" - 아니요.
대신 파일에 기록됩니다.
id_file=$HOME/.local/data/cli-command.id
# delete the old one
cli-command-delete "$(<"$id_file")"
# save the new one
cli-command-create > "$id_file"
답변2
어떤 영구 저장소(파일 등)에 저장하고 스크립트가 시작될 때 읽어야 하며, 읽을 수 있는지 확인하는 것이 좋습니다. 예를 들어:
#!/usr/bin/env sh
id_path=~/.id
oldid="$(cat $id_path)"
if [ -z "$oldid" ]
then
printf "Failed to read oldid from %s\n" "$id_path" >&2
exit 1
fi
cli-command-delete "$oldid"; # here we delete the old id which was generated past day
cli-command-create > "$id_path"