Bash script to tar – Quoting issue [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite
1













This question already has an answer here:



  • Security implications of forgetting to quote a variable in bash/POSIX shells

    3 answers



I have a script that takes filenames as positional parameters. I perform a few operations on these and then tar them. Currently my script is not working. The echo line is there for debugging purposes.




Please clarify this statement



But when I try to tar with in the script if can file the file I want to tar.




SNIPPET



while [[ $# > 0 ]]; do
key="$1"
shift
files=$files" ""$key"
done

echo tar -cvf backup.tar $files
tar -cvf backup.tar $files


OUTPUT:



tar -cvf backup.tar "test.txt"
tar: "test.txt": Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors


I am using the double quotes around the filename (test.txt) as I want to handle files with spaces.



If I were to remove the quotes in the script ("), it will work but then I can’t handle filenames with spaces.



Any ideas?







share|improve this question














marked as duplicate by Scott, peterh, Romeo Ninov, Stephen Rauch, Satō Katsura Oct 14 '17 at 8:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    Why not just pass the file params directly to tar? tar -cvf backup.tar "$@" ?
    – B Layer
    Oct 14 '17 at 0:29










  • what about: files="$files "$key""? Or use an array...files+=("$key") and then tar -cvf backup.tar $files[@]
    – Jesse_b
    Oct 14 '17 at 0:32











  • See But what if …?
    – Scott
    Oct 14 '17 at 0:38










  • Wouldn't he need the $files variable in the tar statement unquoted though? Otherwise, wont they be treated as one argument?
    – Jesse_b
    Oct 14 '17 at 0:44











  • Use capital letters.
    – peterh
    Oct 14 '17 at 1:34














up vote
0
down vote

favorite
1













This question already has an answer here:



  • Security implications of forgetting to quote a variable in bash/POSIX shells

    3 answers



I have a script that takes filenames as positional parameters. I perform a few operations on these and then tar them. Currently my script is not working. The echo line is there for debugging purposes.




Please clarify this statement



But when I try to tar with in the script if can file the file I want to tar.




SNIPPET



while [[ $# > 0 ]]; do
key="$1"
shift
files=$files" ""$key"
done

echo tar -cvf backup.tar $files
tar -cvf backup.tar $files


OUTPUT:



tar -cvf backup.tar "test.txt"
tar: "test.txt": Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors


I am using the double quotes around the filename (test.txt) as I want to handle files with spaces.



If I were to remove the quotes in the script ("), it will work but then I can’t handle filenames with spaces.



Any ideas?







share|improve this question














marked as duplicate by Scott, peterh, Romeo Ninov, Stephen Rauch, Satō Katsura Oct 14 '17 at 8:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    Why not just pass the file params directly to tar? tar -cvf backup.tar "$@" ?
    – B Layer
    Oct 14 '17 at 0:29










  • what about: files="$files "$key""? Or use an array...files+=("$key") and then tar -cvf backup.tar $files[@]
    – Jesse_b
    Oct 14 '17 at 0:32











  • See But what if …?
    – Scott
    Oct 14 '17 at 0:38










  • Wouldn't he need the $files variable in the tar statement unquoted though? Otherwise, wont they be treated as one argument?
    – Jesse_b
    Oct 14 '17 at 0:44











  • Use capital letters.
    – peterh
    Oct 14 '17 at 1:34












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1






This question already has an answer here:



  • Security implications of forgetting to quote a variable in bash/POSIX shells

    3 answers



I have a script that takes filenames as positional parameters. I perform a few operations on these and then tar them. Currently my script is not working. The echo line is there for debugging purposes.




Please clarify this statement



But when I try to tar with in the script if can file the file I want to tar.




SNIPPET



while [[ $# > 0 ]]; do
key="$1"
shift
files=$files" ""$key"
done

echo tar -cvf backup.tar $files
tar -cvf backup.tar $files


OUTPUT:



tar -cvf backup.tar "test.txt"
tar: "test.txt": Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors


I am using the double quotes around the filename (test.txt) as I want to handle files with spaces.



If I were to remove the quotes in the script ("), it will work but then I can’t handle filenames with spaces.



Any ideas?







share|improve this question















This question already has an answer here:



  • Security implications of forgetting to quote a variable in bash/POSIX shells

    3 answers



I have a script that takes filenames as positional parameters. I perform a few operations on these and then tar them. Currently my script is not working. The echo line is there for debugging purposes.




Please clarify this statement



But when I try to tar with in the script if can file the file I want to tar.




SNIPPET



while [[ $# > 0 ]]; do
key="$1"
shift
files=$files" ""$key"
done

echo tar -cvf backup.tar $files
tar -cvf backup.tar $files


OUTPUT:



tar -cvf backup.tar "test.txt"
tar: "test.txt": Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors


I am using the double quotes around the filename (test.txt) as I want to handle files with spaces.



If I were to remove the quotes in the script ("), it will work but then I can’t handle filenames with spaces.



Any ideas?





This question already has an answer here:



  • Security implications of forgetting to quote a variable in bash/POSIX shells

    3 answers









share|improve this question













share|improve this question




share|improve this question








edited Oct 14 '17 at 2:13









G-Man

11.6k82657




11.6k82657










asked Oct 14 '17 at 0:22









goldengreen

62




62




marked as duplicate by Scott, peterh, Romeo Ninov, Stephen Rauch, Satō Katsura Oct 14 '17 at 8:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Scott, peterh, Romeo Ninov, Stephen Rauch, Satō Katsura Oct 14 '17 at 8:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1




    Why not just pass the file params directly to tar? tar -cvf backup.tar "$@" ?
    – B Layer
    Oct 14 '17 at 0:29










  • what about: files="$files "$key""? Or use an array...files+=("$key") and then tar -cvf backup.tar $files[@]
    – Jesse_b
    Oct 14 '17 at 0:32











  • See But what if …?
    – Scott
    Oct 14 '17 at 0:38










  • Wouldn't he need the $files variable in the tar statement unquoted though? Otherwise, wont they be treated as one argument?
    – Jesse_b
    Oct 14 '17 at 0:44











  • Use capital letters.
    – peterh
    Oct 14 '17 at 1:34












  • 1




    Why not just pass the file params directly to tar? tar -cvf backup.tar "$@" ?
    – B Layer
    Oct 14 '17 at 0:29










  • what about: files="$files "$key""? Or use an array...files+=("$key") and then tar -cvf backup.tar $files[@]
    – Jesse_b
    Oct 14 '17 at 0:32











  • See But what if …?
    – Scott
    Oct 14 '17 at 0:38










  • Wouldn't he need the $files variable in the tar statement unquoted though? Otherwise, wont they be treated as one argument?
    – Jesse_b
    Oct 14 '17 at 0:44











  • Use capital letters.
    – peterh
    Oct 14 '17 at 1:34







1




1




Why not just pass the file params directly to tar? tar -cvf backup.tar "$@" ?
– B Layer
Oct 14 '17 at 0:29




Why not just pass the file params directly to tar? tar -cvf backup.tar "$@" ?
– B Layer
Oct 14 '17 at 0:29












what about: files="$files "$key""? Or use an array...files+=("$key") and then tar -cvf backup.tar $files[@]
– Jesse_b
Oct 14 '17 at 0:32





what about: files="$files "$key""? Or use an array...files+=("$key") and then tar -cvf backup.tar $files[@]
– Jesse_b
Oct 14 '17 at 0:32













See But what if …?
– Scott
Oct 14 '17 at 0:38




See But what if …?
– Scott
Oct 14 '17 at 0:38












Wouldn't he need the $files variable in the tar statement unquoted though? Otherwise, wont they be treated as one argument?
– Jesse_b
Oct 14 '17 at 0:44





Wouldn't he need the $files variable in the tar statement unquoted though? Otherwise, wont they be treated as one argument?
– Jesse_b
Oct 14 '17 at 0:44













Use capital letters.
– peterh
Oct 14 '17 at 1:34




Use capital letters.
– peterh
Oct 14 '17 at 1:34










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










If you are always using all the params then just call tar like this: tar -cvf backup.tar "$@". Otherwise, if you are selecting a subset (though you don't show it) then build up the file list in an array like this:



declare -a files
while [[ $# > 0 ]]; do
key="$1"
shift
# assume some filtering goes on here
files+=("$key")
done

tar -cvf backup.tar "$files[@]"





share|improve this answer






















  • excellent this work using the array and quoting when adding to the array. Thanks a million.
    – goldengreen
    Oct 14 '17 at 1:09

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










If you are always using all the params then just call tar like this: tar -cvf backup.tar "$@". Otherwise, if you are selecting a subset (though you don't show it) then build up the file list in an array like this:



declare -a files
while [[ $# > 0 ]]; do
key="$1"
shift
# assume some filtering goes on here
files+=("$key")
done

tar -cvf backup.tar "$files[@]"





share|improve this answer






















  • excellent this work using the array and quoting when adding to the array. Thanks a million.
    – goldengreen
    Oct 14 '17 at 1:09














up vote
3
down vote



accepted










If you are always using all the params then just call tar like this: tar -cvf backup.tar "$@". Otherwise, if you are selecting a subset (though you don't show it) then build up the file list in an array like this:



declare -a files
while [[ $# > 0 ]]; do
key="$1"
shift
# assume some filtering goes on here
files+=("$key")
done

tar -cvf backup.tar "$files[@]"





share|improve this answer






















  • excellent this work using the array and quoting when adding to the array. Thanks a million.
    – goldengreen
    Oct 14 '17 at 1:09












up vote
3
down vote



accepted







up vote
3
down vote



accepted






If you are always using all the params then just call tar like this: tar -cvf backup.tar "$@". Otherwise, if you are selecting a subset (though you don't show it) then build up the file list in an array like this:



declare -a files
while [[ $# > 0 ]]; do
key="$1"
shift
# assume some filtering goes on here
files+=("$key")
done

tar -cvf backup.tar "$files[@]"





share|improve this answer














If you are always using all the params then just call tar like this: tar -cvf backup.tar "$@". Otherwise, if you are selecting a subset (though you don't show it) then build up the file list in an array like this:



declare -a files
while [[ $# > 0 ]]; do
key="$1"
shift
# assume some filtering goes on here
files+=("$key")
done

tar -cvf backup.tar "$files[@]"






share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 14 '17 at 0:42









Jesse_b

10.5k22659




10.5k22659










answered Oct 14 '17 at 0:34









B Layer

3,9241525




3,9241525











  • excellent this work using the array and quoting when adding to the array. Thanks a million.
    – goldengreen
    Oct 14 '17 at 1:09
















  • excellent this work using the array and quoting when adding to the array. Thanks a million.
    – goldengreen
    Oct 14 '17 at 1:09















excellent this work using the array and quoting when adding to the array. Thanks a million.
– goldengreen
Oct 14 '17 at 1:09




excellent this work using the array and quoting when adding to the array. Thanks a million.
– goldengreen
Oct 14 '17 at 1:09


Popular posts from this blog

How to check contact read email or not when send email to Individual?

Christian Cage

How to properly install USB display driver for Fresco Logic FL2000DX on Ubuntu?