简体   繁体   中英

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?

scrip here ... 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. 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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