开发者

How do I create a bash script that accept user input on the same line as the execution of the script?

I guess I'm more of a newb than I thought? :)

The weird thing is, I know this is an easy answer. I know I will be embarrassed at how simple this is. But you can see from the title of the question that I have can't figure out how to even ask the question properly, which is probably why I can't google it... And I know of so many scripts that do this, but I can't seem to find one at the moment that I can view the source of to figure it out.

EDIT: bash script

So here's what I want to accomplish:

user@some-machine:~$ mysc开发者_运维问答ript variable

Where "variable" is something that the user tags on that will become a variable inside the script. I know how to grab user input with the read command, but I want the script to be executed by typing the name of the script followed by the variable, in one line.

I'm being verbose about this because I'm hoping someone else will find this later through a google search and get their answer.

Thanks in advance!

Kevin


the first argument in a bash script is $1, second is $2 etc...


You should check the getopts too, for the unix-like -x argument handling, for example

your_script -d some_d_arg -x file1.txt file2.txt

so distinguish your script's arguments from the filenames.

basic example:

#!/bin/bash
while getopts d:x arg
do
    case "$arg" in
        d) darg="$OPTARG";;
        x) xflag=1;;
        ?) echo >&2 "Usage: $0 [-x] [-d darg] file ..."; exit 1;;
    esac
done
shift $(( $OPTIND-1 ))

for file
do
    echo =$file=
done
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜