Bash Script For Reading User Input then Compressing That Input to Tar file
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
Rep:
'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
'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.
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
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...
Last edited by braveranger; 11-12-2017 at 07:30 AM.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.