LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-11-2017, 06:37 PM   #1
braveranger
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Rep: Reputation: Disabled
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
 
Old 11-11-2017, 06:40 PM   #2
braveranger
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Original Poster
Rep: Reputation: Disabled
encryption

i already the encryption section set up.
 
Old 11-11-2017, 08:06 PM   #3
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
'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
 
Old 11-11-2017, 10:23 PM   #4
braveranger
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by AwesomeMachine View Post
'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.
 
Old 11-12-2017, 12:12 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
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...
 
Old 11-12-2017, 07:28 AM   #6
braveranger
LQ Newbie
 
Registered: Nov 2017
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks, ill give that a try.


Quote:
Originally Posted by michaelk View Post
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.
 
Old 11-12-2017, 07:52 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
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
 
Old 11-12-2017, 11:32 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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.
 
Old 11-12-2017, 06:06 PM   #9
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
cd doesn't work in a script.
 
Old 11-13-2017, 12:48 AM   #10
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
^ 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
 
1 members found this post helpful.
Old 11-13-2017, 09:44 PM   #11
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
Cd works only in the script shell. Or, maybe I'm just imagining that.
 
Old 11-14-2017, 11:34 AM   #12
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Don't all scripts create an instance of a shell?
 
Old 11-15-2017, 08:48 AM   #13
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145

Rep: Reputation: Disabled
try
Code:
file_name=`basename $file_and_path`
file_path=`dirname $file_and_path`

Last edited by rhubarbdog; 11-15-2017 at 08:50 AM. Reason: tags don't seem to be working
 
Old 11-15-2017, 09:36 AM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,699

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 02:57 PM
[SOLVED] bash script to list tar files in directory and then extract based on user's input socalheel Programming 12 09-23-2013 09:24 AM
Store user input in a config file GUI for a BASH script vaniaspeedy Linux - General 1 12-05-2010 12:08 AM
SHell Script Help! When reading input from a user sfmadmax Linux - Newbie 3 12-12-2007 12:03 AM
input into bash script(reading path) dalicros Linux - General 3 05-30-2007 06:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration