Removing ZSH Substring

goal

In the ZSH script for these arguments, I want to get the first line and the rest.

For example, when the script is named test

 sh test hello 

supposed to extract h and ello .

ZSH Guide

http://zsh.sourceforge.net/Doc/zsh_a4.pdf

He speaks:

A signature can also be performed for values ​​without an array, in which case the indices determine the substring to be extracted. For example, if FOO is set to "foobar", then "echo $ FOO [2,5] prints" Ooba.

Q1

So I wrote a shell script in a file called test

 echo $1 echo $1[1,1] 

Terminal:

 $ sh test hello hello hello[1,1] 

the result fails. What is wrong with the code?

Q2

Also I don't know how to extract subString from n to the last. Perhaps I need to use array partitioning with regex?

EDIT: Q3

This may be another question, so if you need to start a new Thread, I will do it.

Thanks @skishore Here is another code

 #! /bin/zsh echo $1 ARG_FIRST=`echo $1 | cut -c1` ARG_REST=`echo $1 | cut -c2-` echo ARG_FIRST=$ARG_FIRST echo ARG_REST=$ARG_REST if $ARG_FIRST = ""; then echo nullArgs else if $ARG_FIRST = "@"; then echo @Args else echo regularArgs fi fi 

I'm not sure how to compare string values ​​with string, but for given arguments hello

result:

 command not found: h 

What is wrong with the code?

EDIT2:

What I found correctly

 #! /bin/zsh echo $1 ARG_FIRST=`echo $1 | cut -c1` ARG_REST=`echo $1 | cut -c2-` echo ARG_FIRST=$ARG_FIRST echo ARG_REST=$ARG_REST if [ $ARG_FIRST ]; then if [ $ARG_FIRST = "@" ]; then echo @Args else echo regularArgs fi else echo nullArgs fi 

EDIT3:

As a result of the whole, this is what I did with this question.

https://github.com/kenokabe/GitSnapShot

GitSnapShot is a thin ZSH shell for Git commands to simplify and simplify use

+6
source share
3 answers

You can use the cut command:

 echo $1 | cut -c1 echo $1 | cut -c2- 

Use $() to assign these values ​​to variables:

 ARG_FIRST=$(echo $1 | cut -c1) ARG_REST=$(echo $1 | cut -c2-) echo ARG_FIRST=$ARG_FIRST echo ARG_REST=$ARG_REST 

You can also replace $() with backlinks, but the former is recommended, and the latter is somewhat outdated due to embedding issues.

+7
source

So I wrote a shell script in a file called test

 $ sh test hello 

This is not a zsh script: you invoke it with sh , which is (almost certainly) bash . If you have shebang ( #!/bin/zsh ), you can make it executable ( chmod +x <script> ) and run it: ./script . Alternatively, you can run it using zsh <script> .

the result fails. What is wrong with the code?

You can enclose in braces:

 echo ${1} # This'll work with or without the braces. echo ${1[3,5]} # This works in the braces. echo $1[3,5] # This doesn't work. 

Running: ./test-script hello gives:

 ./test-script.zsh hello hello llo ./test-script.zsh:5: no matches found: hello[3,5] 

Also I don't know how to extract subString from n to the last. Perhaps I need to use array partitioning with regex?

Use the note [n,last] , but wrap in braces. We can determine how long our variable is and then use the length:

 # Store the length of $1 in LENGTH. LENGTH=${#1} echo ${1[2,${LENGTH}]} # Display from `2` to `LENGTH`. 

This will lead to the creation of ello (2 to the last character hello ).

Script to play with:

 #!/usr/local/bin/zsh echo ${1} # Print the input echo ${1[3,5]} # Print from 3rd->5th characters of input LENGTH=${#1} echo ${1[2,${LENGTH}]} # Print from 2nd -> last characters of input. 

You can use the cut command:

But this will use additional baggage - zsh is quite capable of doing all this at its own level, without creating several subclasses for simplified operations.

+5
source

A1

As others have said, you need to wrap it in braces. In addition, use the shell ( #! ...), mark the file as an executable file and call it directly.

 #!/usr/zsh echo $1 echo ${1[1,1]} 

A2

The easiest way to extract a substring from a parameter (zsh variable parameters call variables) is to use the parameter extension. Using square brackets tells zsh to treat the scalar (i.e., String) parameter as an array. For one character, this makes sense. For the rest of the line, you can use the simpler notation ${parameter:start:length} . If you omit the :length part (as we are here), it will give you the rest of the scalar.

test file:

 #!/usr/zsh echo ${1[1]} echo ${1:1} 

Terminal:

 $ ./test Hello H ello 

A3

As others have said, you need (preferably double) square brackets for testing. Also, to check if the string is not used NULL, use -z , and to check if NULL is used -n . You can simply put the string in double brackets ( [[ ... ]] ), but it is preferable that your intentions be cleared with -n .

 if [[ -z "${ARG_FIRST}" ]]; then ... fi 

RE: EDIT2:

  • Declare all options for setting the scope. If you do not, you can clobber or use a parameter inherited from the shell, which can lead to unexpected behavior. The google style / bash style guide is a good resource for such things.
  • Using built-in external commands.
  • Avoid backlinks. Use $(...) instead.
  • Use single quotes when quoting a string. This prevents patterns from matching.
  • Use elif or case to avoid nested if s. case will be easier to read in your example here, but elif will probably be better for your actual code.

Using case :

 #!/usr/zsh typeset ARG_FIRST="${1[1]}" typeset ARG_REST="${1:1}" echo $1 echo 'ARG_FIRST='"${ARG_FIRST}" echo 'ARG_REST='"${ARG_REST}" case "${ARG_FIRST}" in ('') echo 'nullArgs' ;; ('@') echo '@Args' ;; (*) # Recommended formatting example with more than 1 sloc echo 'regularArgs' ;; esac 

using elif :

 #!/usr/zsh typeset ARG_FIRST="${1[1]}" typeset ARG_REST="${1:1}" echo $1 echo 'ARG_FIRST='"${ARG_FIRST}" echo 'ARG_REST='"${ARG_REST}" if [[ -z "${ARG_FIRST}" ]]; then echo nullArgs elif [[ '@' == "${ARG_FIRST}" ]]; then echo @Args else echo regularArgs fi 

RE: EDIT3

  • Use " $@ " if you really don’t know what you are doing. The explanation .
+3
source

Source: https://habr.com/ru/post/951523/


All Articles