How to properly handle wildcard extension in a bash shell script?

#!/bin/bash

hello()
{
    SRC=$1
    DEST=$2

    for IP in `cat /opt/ankit/configs/machine.configs` ; do
        echo $SRC | grep '*' > /dev/null
        if test `echo $?` -eq 0 ; then
            for STAR in $SRC ; do
                echo -en "$IP"
                echo -en "\n\t ARG1=$STAR ARG2=$2\n\n"
            done
        else
            echo -en "$IP"
            echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
        fi
    done
}

hello $1 $2

The above script shell in which I provide the source path (SRC) and desitnation (DEST). It worked great when I didn't insert the SRC path with a wild card. '' When I run this shell script and give '.pdf or' * 'as follows:

root@ankit1:~/as_prac# ./test.sh /home/dev/Examples/*.pdf /ankit_test/as

I get the following output:

192.168.1.6
ARG1=/home/dev/Examples/case_Contact.pdf ARG2=/home/dev/Examples/case_howard_county_library.pdf

DEST is / ankit_test /, but DEST also gets manupulated due to '*'. Expected response

ARG1=/home/dev/Examples/case_Contact.pdf ARG2=/ankit_test/as

So, if you understand what I'm trying to do, please help me solve this problem. I would be grateful to you.

Thanks in advance!

I need to know exactly how I use "* .pdf" in my program one by one, without breaking DEST.

+3
source share
9 answers

script . . :

ARG1=/home/dev/Examples/*.pdf ARG2=/ankit__test/as

:

for IP in `cat /opt/ankit/configs/machine.configs`
do
    for i in $SRC
    do
        echo -en "$IP"
        echo -en "\n\t ARG1=$i ARG2=$DEST\n\n"
    done
done

:

root@ankit1:~/as_prac# ./test.sh "/home/dev/Examples/*.pdf" /ankit__test/as
+4

, , ,

$ ls
one.pdf two.pdf three.pdf

script

./test.sh *.pdf /ankit__test/as

,

./test.sh one.pdf two.pdf three.pdf /ankit__test/as

.

./test.sh \*.pdf /ankit__test/as

.

+3

, , script :

./test.sh /ankit_test/as /home/dev/Examples/*.pdf

, . script , :

#!/bin/bash
hello()
{
    SRC=$1
    DEST=$2

    for IP in `cat /opt/ankit/configs/machine.configs` ; do
        echo -en "$IP"
        echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
    done
}

arg2=$1
shift
while [[ "$1" != "" ]] ; do
        hello $1 $arg2
        shift
done
+2

"", .

+1

, , :

#!/bin/bash

hello() {

  SRC=$1
  DEST=$2

   while read IP ; do
     for FILE in $SRC; do
       echo -e "$IP"
       echo -e "\tARG1=$FILE ARG2=$DEST\n"
      done
   done < /tmp/machine.configs
 }

 hello "$1" $2
  • , script
  • hello, $1 , , , $SRC
+1

:

#!/bin/bash

hello()
{
    # DEST will contain the last argument
    eval DEST=\$$#

    while [ $1 != $DEST ]; do
        SRC=$1

        for IP in `cat /opt/ankit/configs/machine.configs`; do
            echo -en "$IP"
            echo -en "\n\t ARG1=$SRC ARG2=$DEST\n\n"
        done

        shift || break
    done
}

hello $*

hello() , script.

hello() DEST var. , SRC , , SRC DEST. , $SRC $DEST, . , SRC , DEST, , ( ).

+1

, *.txt, , , . , bash "ls" "rm". , 3 , , .

( ls)

file1.txt file2.txt file3.txt

script

$ ./script.sh *.txt

$ ./script.sh file{1..3}.txt

script

#!/bin/bash

# store default IFS, we need to temporarily change this
sfi=$IFS

#set IFS to $'\n\' - new line
IFS=$'\n'

if [[ -z $@ ]]
  then
  echo "Error: Missing required argument"
  echo
  exit 1
fi

# Put the file glob into an array
file=("$@")

# Now loop through them
for (( i=0 ; i < ${#file[*]} ; i++ ));
do

  if [ -w ${file[$i]} ]; then
     echo ${file[$i]} "  writable" 
  else
     echo ${file[$i]} " NOT writable"
  fi
done

# Reset IFS to its default value
IFS=$sfi

file1.txt writable
file2.txt writable
file3.txt writable

IFS ( ). , , , , .

( escaped) file [], . , , .

+1

$?, .

:

if [ $? -eq 0 ]; then
0

./test.sh /home/dev/Examples/*.pdf /ankit_test/as 

, script. ,

./test.sh "/home/dev/Examples/*.pdf" /ankit_test/as

and then in the script, quote "$ SRC" anywhere where you literally want to use wildcards (i.e. when you do echo $SRC, use it instead echo "$SRC") and leave it without quotes, the templates are expanded. Basically, always put quotes around things that may contain shell metacharacters if you don't want the metacharacters to be interpreted. :)

0
source

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


All Articles