How to read command line argument in shell sript

pri

I need to read command line argument which is passing as scriptname –c "30,31,32,33,34,35"

and convert it to

myArray=( 30 31 32 )

cdarke

Try the following:

while getopts c: option
do
    case $option in
       c) data="$OPTARG"
          ;;
    esac
done

oldIFS="$IFS"
IFS=','
myArray=($data)
IFS="$oldIFS"

echo ${myArray[@]}

The c: after getoptsindicates that we have an option -c, the : indicates it is followed by an argument, which is retrieved using $OPTARG.

IFS if the Inter Field Separator which I reset to a comma in order to create the array.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How does command to put into shell argument at VI?

분류에서Dev

How to restart GNOME Shell from command line?

분류에서Dev

How to count the number of characters in a command line argument by using and creating a function

분류에서Dev

How to grep command line argument on which RegEx has been applied?

분류에서Dev

Splitting bash command line argument

분류에서Dev

How to paste complex passwords into windows command line (shell)?

분류에서Dev

How to read values from command line in bash script as given?

분류에서Dev

Parse command line argument using argparse

분류에서Dev

Adding file path as Command Line Argument

분류에서Dev

Is there a 'Restore session' command-line argument for Firefox?

분류에서Dev

Optional command line argument in R script

분류에서Dev

How to pass a terminal command as an argument to another command

분류에서Dev

use 'mail' to read email from command line

분류에서Dev

getchar to read from command line arguments

분류에서Dev

How would I read the contents of a text file and run a command for each line?

분류에서Dev

How to output a shell command into a variable?

분류에서Dev

Read Strings from command line util one can be 'read' as a Float

분류에서Dev

"substring not found" even though it's included in the argument passed on the command line

분류에서Dev

How do you read an argument into an array

분류에서Dev

How to make a for loop in command line?

분류에서Dev

While read from line in shell script sh linux

분류에서Dev

Configuring Gnome Shell key bindings from the command line

분류에서Dev

Posix shell script - Save multi line command output to variable

분류에서Dev

How to give python command line arguments from command line mode

분류에서Dev

How to tell shell command tail not to wait for parameter

분류에서Dev

How to perform arithmetic operations on the output of a shell command

분류에서Dev

how to cat command output to string in shell script

분류에서Dev

How to pass variable to shell command in C?

분류에서Dev

How execute specific command from shell script

Related 관련 기사

  1. 1

    How does command to put into shell argument at VI?

  2. 2

    How to restart GNOME Shell from command line?

  3. 3

    How to count the number of characters in a command line argument by using and creating a function

  4. 4

    How to grep command line argument on which RegEx has been applied?

  5. 5

    Splitting bash command line argument

  6. 6

    How to paste complex passwords into windows command line (shell)?

  7. 7

    How to read values from command line in bash script as given?

  8. 8

    Parse command line argument using argparse

  9. 9

    Adding file path as Command Line Argument

  10. 10

    Is there a 'Restore session' command-line argument for Firefox?

  11. 11

    Optional command line argument in R script

  12. 12

    How to pass a terminal command as an argument to another command

  13. 13

    use 'mail' to read email from command line

  14. 14

    getchar to read from command line arguments

  15. 15

    How would I read the contents of a text file and run a command for each line?

  16. 16

    How to output a shell command into a variable?

  17. 17

    Read Strings from command line util one can be 'read' as a Float

  18. 18

    "substring not found" even though it's included in the argument passed on the command line

  19. 19

    How do you read an argument into an array

  20. 20

    How to make a for loop in command line?

  21. 21

    While read from line in shell script sh linux

  22. 22

    Configuring Gnome Shell key bindings from the command line

  23. 23

    Posix shell script - Save multi line command output to variable

  24. 24

    How to give python command line arguments from command line mode

  25. 25

    How to tell shell command tail not to wait for parameter

  26. 26

    How to perform arithmetic operations on the output of a shell command

  27. 27

    how to cat command output to string in shell script

  28. 28

    How to pass variable to shell command in C?

  29. 29

    How execute specific command from shell script

뜨겁다태그

보관