LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Script For Reading User Input then Compressing That Input to Tar file (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-for-reading-user-input-then-compressing-that-input-to-tar-file-4175617444/)

braveranger 11-11-2017 06:37 PM

Bash Script For Reading User Input then Compressing That Input to Tar file
 
I have this so far:
#!/bin/bash

echo "Do you wish to encode or decode?"
select yn in "Encode" "Decode"; do
case $yn in
Encode ) echo "Name the file/text to be encoded:"
read name

echo "Drag files or type text to be compressed and ecoded now"
read raw

tar -czf $name.tar.gz $raw
in the terminal, i drag the files into the terminal at the "Drag files..." dialog

the problem i'm having is that when this runs, it compresses with the file(s) buried in the path. How to I change it to only compress the files and not the path? The strip option doesn't work.

Thanks

braveranger 11-11-2017 06:40 PM

encryption
 
i already the encryption section set up.

AwesomeMachine 11-11-2017 08:06 PM

'Read raw' is not going to work. The -r switch to the read builtin means 'raw', which disables backslash escapes and line continuation in the command line. If you want to use 'read', see this: http://wiki.bash-hackers.org/commands/builtin/read

braveranger 11-11-2017 10:23 PM

Quote:

Originally Posted by AwesomeMachine (Post 5779643)
'Read raw' is not going to work. The -r switch to the read builtin means 'raw', which disables backslash escapes and line continuation in the command line. If you want to use 'read', see this: http://wiki.bash-hackers.org/commands/builtin/read

i changed "raw" to "files" and tried several variations of read and still having the same issue. when opening archive, i have multiple directories down to the file, like /home/user/test/test.txt. i want it to just be the file, with no directories.

michaelk 11-12-2017 12:12 AM

Including the absolute path would be the function of the desktop. If you remove the path then your script would fail if the files were not located in the same directory as your script.

Striping the path in tar is not so easy. One convoluted method would be to strip the path from the file in a loop, use the tar -C option and append the file to your archive then compress. Untested psuedo code would be something like.
Code:

For x in $files
do
  y=path of $x
  z=file name of $x
  tar -Af $name.tar -C $y $z
done
gzip $name.tar

There maybe a simpler method...

braveranger 11-12-2017 07:28 AM

Thanks, ill give that a try.


Quote:

Originally Posted by michaelk (Post 5779670)
Including the absolute path would be the function of the desktop. If you remove the path then your script would fail if the files were not located in the same directory as your script.

Striping the path in tar is not so easy. One convoluted method would be to strip the path from the file in a loop, use the tar -C option and append the file to your archive then compress. Untested psuedo code would be something like.
Code:

For x in $files
do
  y=path of $x
  z=file name of $x
  tar -Af $name.tar -C $y $z
done
gzip $name.tar

There maybe a simpler method...


michaelk 11-12-2017 07:52 AM

Here is one quick method to strip path and filename but it will fail if they contain spaces. It also strips single quotes.

Code:

  temp="${x%\'}"
  temp="${temp#\'}"
  echo "${temp%/*}" # path to file
  basename $temp    # file name


ondoho 11-12-2017 11:32 AM

couldn't the script cd to the folder in question, then tar the file?
you'd have to separate the input into path and filename.

also for troubleshooting i recommend prepending th elast command with 'echo' to see what would have been tar'd.

AwesomeMachine 11-12-2017 06:06 PM

cd doesn't work in a script.

ondoho 11-13-2017 12:48 AM

^ what?
that would mean a significant number of my scripts wouldn't work.
no really, it does work on my setup: GNU bash, version 4.4.12

AwesomeMachine 11-13-2017 09:44 PM

Cd works only in the script shell. Or, maybe I'm just imagining that.

scasey 11-14-2017 11:34 AM

Don't all scripts create an instance of a shell?

rhubarbdog 11-15-2017 08:48 AM

try
Code:

file_name=`basename $file_and_path`
file_path=`dirname $file_and_path`


michaelk 11-15-2017 09:36 AM

Much easier then my suggestion. Although a better method would be to use
Code:

file_name=$(basename "$files")
file_path=$(dirname "files")

Yes cd works in a script. As stated a script is ran with its own instance of a shell which means that when it terminates you will still be in the same directory as where you ran the script.


All times are GMT -5. The time now is 05:41 PM.