What is the purpose of using shift in shell scripts?

Patryk

I have came across this script:

#! /bin/bash                                                                                                                                                                                           

if (( $# < 3 )); then
  echo "$0 old_string new_string file [file...]"
  exit 0
else
  ostr="$1"; shift
  nstr="$1"; shift  
fi

echo "Replacing \"$ostr\" with \"$nstr\""
for file in $@; do
  if [ -f $file ]; then
    echo "Working with: $file"
    eval "sed 's/"$ostr"/"$nstr"/g' $file" > $file.tmp 
    mv $file.tmp $file
  fi  
done

What is the meaning of the lines where they use shift? I presume the script should be used with at least arguments so...?

humanityANDpeace

shift is a bash built-in which kind of removes arguments from the beginning of the argument list. Given that the 3 arguments provided to the script are available in $1, $2, $3, then a call to shift will make $2 the new $1. A shift 2 will shift by two making new $1 the old $3. For more information, see here:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Purpose of square brackets in shell scripts

From Dev

What purpose does using exec in docker entrypoint scripts serve?

From Dev

what is the main purpose of using pipe symbol in shell scripting?

From Dev

What is the intended purpose of a login shell?

From Java

What is the purpose of using HKDF?

From Dev

What is Purpose of @using in BeginForm

From Dev

What is the purpose of using ~ in JavaScript?

From Dev

What is Purpose of @using in BeginForm

From Dev

What is the use of portable shell scripts?

From Dev

What's the meaning of these shell scripts?

From Dev

What is the purpose of (and possibly the convention of using) multiple locations for user configuration in fish shell

From Dev

What is the purpose of (and possibly the convention of using) multiple locations for user configuration in fish shell

From Dev

What is the purpose of using session in WCF

From Dev

BASH: What is the purpose of using `/` in a list?

From Dev

What is the purpose of using nginx with gunicorn?

From Dev

what is the purpose of using JSON in android?

From Dev

BASH: What is the purpose of using `/` in a list?

From Java

What are the uses of the exec command in shell scripts?

From Dev

What are the rules to write robust shell scripts?

From Dev

What are my options for integrating audacity into shell scripts?

From Dev

what could be the reasons for shell scripts kill itself?

From Dev

What does ${0%/*} in shell scripts do?

From Dev

What does the operator `-gt` in shell scripts mean?

From Dev

Using Travis CI for testing on UNIX shell scripts

From Dev

Reverse hexadecimal number using shell scripts

From Dev

Using “reserved” codes for exit status of shell scripts

From Dev

Using “reserved” codes for exit status of shell scripts

From Dev

Reverse hexadecimal number using shell scripts

From Dev

Using Travis CI for testing on UNIX shell scripts

Related Related

  1. 1

    Purpose of square brackets in shell scripts

  2. 2

    What purpose does using exec in docker entrypoint scripts serve?

  3. 3

    what is the main purpose of using pipe symbol in shell scripting?

  4. 4

    What is the intended purpose of a login shell?

  5. 5

    What is the purpose of using HKDF?

  6. 6

    What is Purpose of @using in BeginForm

  7. 7

    What is the purpose of using ~ in JavaScript?

  8. 8

    What is Purpose of @using in BeginForm

  9. 9

    What is the use of portable shell scripts?

  10. 10

    What's the meaning of these shell scripts?

  11. 11

    What is the purpose of (and possibly the convention of using) multiple locations for user configuration in fish shell

  12. 12

    What is the purpose of (and possibly the convention of using) multiple locations for user configuration in fish shell

  13. 13

    What is the purpose of using session in WCF

  14. 14

    BASH: What is the purpose of using `/` in a list?

  15. 15

    What is the purpose of using nginx with gunicorn?

  16. 16

    what is the purpose of using JSON in android?

  17. 17

    BASH: What is the purpose of using `/` in a list?

  18. 18

    What are the uses of the exec command in shell scripts?

  19. 19

    What are the rules to write robust shell scripts?

  20. 20

    What are my options for integrating audacity into shell scripts?

  21. 21

    what could be the reasons for shell scripts kill itself?

  22. 22

    What does ${0%/*} in shell scripts do?

  23. 23

    What does the operator `-gt` in shell scripts mean?

  24. 24

    Using Travis CI for testing on UNIX shell scripts

  25. 25

    Reverse hexadecimal number using shell scripts

  26. 26

    Using “reserved” codes for exit status of shell scripts

  27. 27

    Using “reserved” codes for exit status of shell scripts

  28. 28

    Reverse hexadecimal number using shell scripts

  29. 29

    Using Travis CI for testing on UNIX shell scripts

HotTag

Archive