变量语法错误使用bash脚本(作业) [英] Variable Syntax Error with Bash Script (Homework)

查看:100
本文介绍了变量语法错误使用bash脚本(作业)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在做功课了Unix的课程,我已经得到了一切做,但我只是想不出我做了什么错在这里。我已经做了在csh此相同的脚本(但我们需要做的是在bash为好。)我已经修正了一些错误,但现在的这个我得到是可变语法当我尝试运行它

我一直在使用双括号内为同时和IFS尝试
我也试着为声​​明输入的变量之前我试着读给他们
我也试着窃听几个人在我的课,但他们大多是pretty无能,只是复制别人的油。

 #/!斌/庆典

#Menunix - 巴什

#Usage:menunixb
输入=$(1)
而[$输入!= 5]

    回声请选择一个选项
    回音1:列表文件
    回音2:显示当天的日期和时间
    回音3:检查文件是否是一个目录或不
    回声4:创建一个文件备份
    回声5:退出
    读取输入
    案例$输入
        1)
            LS
            ;;
        2)
            呼应-n时间:
            日期+%T
            回声日期:
            日期+%D
            ;;
        3)
            回声是什么文件,你要检查
            阅读finput
            如果[-d $ finput]。然后
                回声$ finput是一个目录
            ELIF [-f $ finput]。然后
            回声$ finput是一个文件
            其他
                回声$ finput不存在
                ;;
            4)
                回声请输入文件名备份
                阅读binput
                CP $ {binput。李明博}
                ;;
            5)
                1号出口
            *)
                回声请选择一个有效的输入
                1号出口
            ESAC
        DONE
#EOF


解决方案

有一个看看:

 输入=$(1)

您注意到周围的等号(=)的空间?这不是在bash允许。它必须是

 输入=$(1)

代替。

I'm currently working on homework for a Unix course and I've got everything else done but I just can't figure out what I've done wrong here. I've done this exact same script in csh (but we need to do it in bash as well.) I've already fixed a few errors but now the one I'm getting is "Variable Syntax" when I try to run it.

I've tried using double brackets for the while and ifs I've also tried declaring the variables for the inputs before I try to read to them I've also tried bugging several people in my class but most of them are pretty clueless and just copy off others.

#/!bin/bash
#
#Menunix - Bash
#
#Usage: menunixb
input = "$(1)" 
while [ $input != 5 ]
do
    echo Please choose an option
    echo 1: List Files 
    echo 2: Display today's date and time
    echo 3: Check whether a file is a directory or not
    echo 4: Create a file backup
    echo 5: Quit
    read input
    case $input in
        1) 
            ls
            ;;
        2)
            echo -n Time:
            date +"%T"
            echo Date:
            date +"%D"
            ;;
        3)
            echo What file do you wish to check
            read finput
            if [ -d $finput ] ; then
                echo $finput is a Directory
            elif [ -f $finput ] ; then
            echo $finput is a File
            else
                echo $finput does not exist
                ;;
            4)
                echo Please enter filename to backup
                read binput
                cp $binput{,.bak}
                ;;
            5)
                exit 1
            *)
                echo Please choose a valid input
                exit 1
            esac
        done
#EOF

解决方案

Have a look at:

input = "$(1)"

You notice the spaces around the equal sign (=)? This is not allowed in bash. It has to be

input="$(1)"

instead.

这篇关于变量语法错误使用bash脚本(作业)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆