【问题标题】:If elif else not working in bash ChromeOS如果 elif else 在 bash ChromeOS 中不起作用
【发布时间】:2019-06-09 13:30:10
【问题描述】:

我正在尝试制作一个 bash 脚本,它基本上需要一堆 .deb,解压缩它们并将二进制文件和库放在 /usr/local/opt/{lib}bin 中。

脚本检查 / 是挂载为 ro 还是 rw,如果挂载为 ro 则重新挂载为 rw。 然而,在 chromebook 上,为了将 / 挂载为 rw,您需要为有问题的分区删除_rootfs_verification。当为 / 启用 rootfs_verification 时,脚本无法回显上述内容,应该退出 1,而是继续执行。

这是我所指的脚本部分

### ChromeOS's Specific!!!
# The following assumes rootfs_verification for / has already been removed 
if grep $rootfs /proc/mounts | grep ro; then
  mount -o remount,rw / &> mount.out 

elif 
 grep -iw 'read-write' mount.out; then 

   echo '\nrootfs_verification for the root partition must to be removed in order to remount,rw  /
To remove rootfs_verification run the following command and than reboot the system:
"sudo /usr/share/vboot/bin/make_dev_ssd.sh  --remove_rootfs_verification --partitions 4"'

else exit 1

fi

完整的 WIP 脚本可以在这里找到https://pastebin.com/ekEPSvYy

这就是我执行它时发生的情况

localhost /usr/local # ./kvm_install.sh 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   124    0   124    0     0    322      0 --:--:-- --:--:-- --:--:--   345
100   135  100   135    0     0    170      0 --:--:-- --:--:-- --:--:--   170
100 60384  100 60384    0     0  57950      0  0:00:01  0:00:01 --:--:--  344k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   143    0   143    0     0    407      0 --:--:-- --:--:-- --:--:--   412
100   154  100   154    0     0    202      0 --:--:-- --:--:-- --:--:--   202
100 1298k  100 1298k    0     0   929k      0  0:00:01  0:00:01 --:--:-- 3020k
/dev/root / ext2 ro,seclabel,relatime,block_validity,barrier,user_xattr,acl 0 0
./kvm_install.sh: line 31: /etc/env.d/30kvm: Read-only file system
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 66802  100 66802    0     0  69657      0 --:--:-- --:--:-- --:--:-- 74555
./kvm_install.sh: line 39: ar: command not found
tar (child): control.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
md5sum: md5sums: No such file or directory

基本上这里发生的情况是找不到 ar,因为脚本无法将 PATH 变量添加到 /etc/env.d/30kvm,因为根分区无法挂载,因为 / 上启用了 root_verification。

我尝试按照此处的一些建议在 [[ 中添加 elif "grep" 命令,但这不起作用并增加了更多语法问题。

我正在学习 bash 脚本的基础知识。如果剧本写得不好,我深表歉意。

谢谢

我最终还是这样做了。

 if grep $rootfs /proc/mounts | grep 'ro,'; then
      mount -o remount,rw / &> mount.out

if
    grep 'read-write' mount.out; then
     echo 'something to echo' && exit 1
    fi
    fi

它并不漂亮,但在我找到/学习更好的方法来实现循环之前它可以工作。

【问题讨论】:

  • mount.out 仅在 if 部分中创建。如果elif 部分中有可供阅读的内容,则它不是直接在上面创建的内容,而是来自您之前成功的if 运行。那可不好;-) 我对mount 选项做得还不够,无法提供有用的cmets。似乎您理解了这个问题,但我会添加像echo "inside elif" 这样的调试语句,这样您就可以确定脚本遵循您期望的路径。祝你好运。
  • 还要注意grep ro 将匹配/dev/root / ext2 rw 以及/dev/root / ext2 ro
  • @shellter 我不完全确定您的意思:“mount.out 仅在 if 部分中创建。如果在 elif 部分中有可供阅读的内容,则不是创建的内容直接在上面,是你之前成功的if run。" 谢谢
  • 我应该在评论前加上“基于您提供的代码”,但现在我记得您只包含了部分脚本。在任何情况下,当您在elif 部分检查它时,您的mount.out 可能会受到怀疑,因此请确保其中包含最新信息。如果您仍然遇到问题,我认为我的调试想法是您应该从哪里开始。祝你好运。

标签: bash google-chrome-os chromebook


【解决方案1】:

使命令的输出成为变量:

$variable="$(command)"

如果你想使用 grep 使用语法:

command | grep text

如果要执行 if 语句,请使用以下语法:

if [ some text ]; text
    commands
elif [ some text ]
    commands
else
    commands
fi

for the some text check this cart

grep -iw 在 chromebook 上无效

rootfs 有不同的路径,这取决于很多事情,如果你想将它保存为 $rootfs 执行此命令

rootfs="$(rootdev -s)"

你也犯了一些错误是“|”和“&”

command1 | command to edit command1
command1 || run this command if command1 fails
command1 && run this command if command1 succeeds 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 2017-06-29
    • 2018-03-25
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 2023-01-25
    相关资源
    最近更新 更多