简体   繁体   English

我如何将参数发送到bash脚本

[英]How do i send a argument to a bash script

I have a bash script, that I want to either print the result out or just run the script and not print the result (a script that checks os type (linux, ubuntu, debian and others). Depending on what i send (./script P (just an example)) i want it to echo the info to the screen. How do I do that easily? 我有一个bash脚本,我想将结果打印出来或只运行脚本而不打印结果(一个检查操作系统类型(Linux,Ubuntu,Debian和其他操作系统)的脚本。脚本P(仅作为示例))我希望它在屏幕上显示信息。

scrip here ... shootProfile 在这里抄写... shootProfile

#echo "OS: $OS"
#echo "DIST: $DIST"
#echo "PSUEDONAME: $PSUEDONAME"
#echo "REV: $REV"
#echo "DistroBasedOn: $DistroBasedOn"
#echo "KERNEL: $KERNEL"
#echo "MACH: $MACH"
#echo "========"

... and after here ...然后在这里

so if I send ./script P , the #echo should just be echo.. or something similar to get that output to screen. 因此,如果我发送./script P,则#echo应该只是echo ..或类似的东西来将输出输出到屏幕。 I feel like a total noob right now :/ 我现在感觉像个菜鸟:/

You send it by invoking like this: 您可以通过如下调用来发送:

$ ./myscript.sh "parameter"

And you pick it from the script like that: 然后从这样的脚本中选择它:

#!/bin/sh
echo "This is the first parameter: $1"

Additionally, you can use some conditionals: 此外,您可以使用一些条件:

if test "$1" = "P"
then
    echo "You said P"
fi

I believe you are looking for something like: 我相信您正在寻找类似的东西:

for arg in "$@" ; do
   case "$arg" in
      P|p) echo "PSEUDONAME: $PSEUDONAME" ;;
      O|o) echo "OS: $OS"                 ;;
      *) echo "I did not implement the recognition of : ${arg}"
         echo "Please correct the invocation of the script, or the script itself"
         echo "I STOP HERE."
         exit 1
         ;;
    esac
done

This will allow you to specify several command after the script's name, and they will be all treated (or, if you forgot the necessary recognition of one of them, will output a message and then exit instead of continue in a "weird state") 这将允许您在脚本名称后指定几个命令,并且将全部处理它们(或者,如果您忘记了其中一个的必要识别,则将输出一条消息,然后退出而不是继续处于“怪异状态”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM