LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-11-2017, 10:46 AM   #1
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 241

Rep: Reputation: Disabled
Want to "slim down" a bash script.


This is the original script.
Code:
#!/bin/bash
##Exit from the if/then loop with a lower-case X
##Don't mind the insult.
file0=$@
if [ ! -n $file0 ]; then
	echo -en "No file named to get a date from.\nLet's try again.\nEnter a file name."
	read file0
	if [ $file0= "x" ]; then 
		echo "Sod off, rotter!"
		exit 0
	fi
fi
echo "Finds and formats your file date (for file $file0) to use with a 'touch -t' command."
if [ ! -n $file0 ]; then
	echo -en "No file named to get a date from.\nLet's try again.\nEnter a file name."
	read file0
fi
dart=$(/usr/bin/stat -c %y "$file0" | /usr/bin/cut -d. -f1)
dart=${dart//-/}
dart=${dart/ /}
dart2=${dart/:/}
dart=${dart2/:/.}
echo $dart | /usr/bin/xsel -ib
echo -e "Your date (with no delimiters or spaces is:\n\e[7m$dart\e[0m"
echo "It is also on the clipboard -- check Klipper."
I'm sure you can awk or sed lines 19 to 22 to eliminate them, or use something else native to the shell, but I have (first) forgotten how and (second) can't seem to find the procedure on the Web; reading/interpreting the GNU pages on string manipulation got me where I am in the first place.

Carver
 
Old 05-11-2017, 10:58 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
For one in your comments. it is not a loop of any kind because it does not keep repeating itself or runs in a cycle.

sed and awk are not native to the shell. they are called into the shell to be used within it. (someone correct me if I am wrong on that one).


first run
Code:
userx%slackwhere ⚡ scripts ⚡> chmod +x wtf
userx%slackwhere ⚡ scripts ⚡> ./wtf
Finds and formats your file date (for file ) to use with a 'touch -t' command.
/usr/bin/stat: cannot stat '': No such file or directory
./wtf: line 23: /usr/bin/xsel: No such file or directory
Your date (with no delimiters or spaces is:

It is also on the clipboard -- check Klipper.
second run
Code:
userx%slackwhere ⚡ scripts ⚡> ./wtf testfile
Finds and formats your file date (for file testfile) to use with a 'touch -t' command.
./wtf: line 23: /usr/bin/xsel: No such file or directory
Your date (with no delimiters or spaces is:
201705102008.24
It is also on the clipboard -- check Klipper.
the error no file etc.. is account of I don't got xsel


test file
Code:
testfile
id:6
amount:1032
three-there
sed to remove hyphen
Code:
userx%slackwhere ⚡ scripts ⚡> sed 's/-/ /g' testfile
testfile
id:6
amount:1032
three there
sed to replace : colon with . period
Code:
userx%slackwhere ⚡ scripts ⚡> sed 's/:/./g' testfile                                                      
testfile
id.6
amount.1032
three-there
Notice much difference?

Last edited by BW-userx; 05-11-2017 at 11:05 AM.
 
Old 05-11-2017, 01:34 PM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,237
Blog Entries: 10

Rep: Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160
i'm intrigued.
what is the script doing? or supposed to?
 
Old 05-13-2017, 01:24 AM   #4
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 241

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
i'm intrigued.
what is the script doing? or supposed to?
I often download JPEGs I want to keep the server-side modification dates for. This script helps me do so.
Code:
echo "Finds and formats your file date (for file $file0) to use with a 'touch -t' command."
I don't know about your bash, but mine invariably gives me something like
Code:
2017-05-11 23:22:53.553544345 -0400
when I run a stat -c %y" command on a file. My touch with the -t option gives an error on that kind of string, and I'm looking to avoid typing spaces (a string of digits saves time and effort, imo) in the dates I set for changing the dates back after I've run some command-line application such as ImageMagick on them. I used to like touch -d and thought it was OK (i.e., didn't return "Invalid format" errors) with just about every date string I could use, and rather than parse out the date block from an ls command, I figured using stat -c %y gave me the same information. I couldn't find a stat option that gave the date without some kind of delimiter (excepting the one for seconds from the Epoch), so I found string manipulation such as that in the original script a good enough way to go. But as you can see, it costs a lot in lines of code, and I want to "trim" it "down" to something more 'efficient' and (hopefully, too) 'adaptable' in case the maintainers of bash decide to change the rules again (as they obviously did with customizing field separators, and that just since bash 4.0).

I hope this explains what the script is for.

Carver

Last edited by L_Carver; 05-13-2017 at 01:25 AM.
 
Old 05-13-2017, 04:36 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,237
Blog Entries: 10

Rep: Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160
the script works for me.
i improved it a little (and removed the pointless insult):
Code:
#!/bin/bash

f="$@"

stat_cmd="$(which stat)"
xsel_cmd="$(which xsel)"

[[ "x$stat_cmd" == "x" ]] && exit 1

while [ ! -f "$f" ]; do
	printf "No file named to get a date from.\nLet's try again.\nEnter a file name."
	read f
done

echo "Finds and formats your file date (for file $f) to use with a 'touch -t' command."
if [ ! -n $f ]; then
	echo -en "No file named to get a date from.\nLet's try again.\nEnter a file name."
	read f
fi
dart=$($stat_cmd -c %y "$f")
dart=${dart%%.*}
dart=${dart//-/}
dart=${dart/ /}
dart=${dart/:/}
dart=${dart/:/.}
echo $dart | $xsel_cmd -ib
echo -e "Your date (with no delimiters or spaces is:\n\e[7m$dart\e[0m"
echo "It is also on the clipboard -- check Klipper."
exit 0
i'm sorry i renamed file0 to f - it has no meaning for you, but i'm too lazy to change it back.
you don't need cut; another bash string manip can do that.
instead of using hardcoded paths, i instructed the script to find the actual absolute path to the executable.
i think it's better that way.
the script won't fail if xsel isn't installed.
the output i'm getting is:
Code:
$> testy testfile
Finds and formats your file date (for file testfile) to use with a 'touch -t' command.
Your date (with no delimiters or spaces is:
201705131236.21
It is also on the clipboard -- check Klipper.
isn't that sufficient?
 
Old 05-13-2017, 04:06 PM   #6
L_Carver
Member
 
Registered: Sep 2016
Location: Webster MA USA
Posts: 241

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho
isn't that sufficient?
Indeed. And it may solve the other problem I noticed about this script: when a filename (or variable with a filename as value) is skipped or not entered, the call to "stat" generates a "File not found" error, which I solved another way. The updated part of the script is:
Code:
dart=$(/usr/bin/stat -c %y "$file0" 2>/dev/null | /usr/bin/cut -d. -f1)
Previously, I had the error dump at the end of line 18 (above). I moved it to just after the stat command and now it works.

Quote:
Originally Posted by ondoho
you don't need cut; another bash string manip can do that.
I see that, but just to be sure, is it
Code:
dart=${dart%%.*}
??

Thanks for the help.

Carver
 
Old 05-13-2017, 05:36 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
this is what ya get
Code:
userx%voider ⚡ ~ ⚡> dots=onedot.twodot.threedot.
userx%voider ⚡ ~ ⚡> echo $dots
onedot.twodot.threedot.
userx%voider ⚡ ~ ⚡> cutdot=${dots%%.*}
userx%voider ⚡ ~ ⚡> echo $cutdot
onedot
userx%voider ⚡ ~ ⚡>
cut farthest from the right.
Code:
userx%voider ⚡ ~ ⚡> cutdot=${dots##*.}
userx%voider ⚡ ~ ⚡> echo $cutdot

userx%voider ⚡ ~ ⚡>
cuts farthest from the left

Last edited by BW-userx; 05-13-2017 at 05:38 PM.
 
Old 05-14-2017, 01:07 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,237
Blog Entries: 10

Rep: Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160
Quote:
Originally Posted by L_Carver View Post
I see that, but just to be sure, is it
Code:
dart=${dart%%.*}
??
yes.
quick version:
http://commandliners.com/2015/04/str...ation-in-bash/
in depth:
http://www.tldp.org/LDP/abs/html/str...ipulation.html
 
Old 05-28-2017, 12:05 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,599

Rep: Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933Reputation: 1933
Code:
dart=$(/usr/bin/stat -c %y "$file0" | /usr/bin/cut -d. -f1)
dart=${dart//-/}
dart=${dart/ /}
dart2=${dart/:/}
dart=${dart2/:/.}
Should be the same as

Code:
dart=$(/usr/bin/date --reference="$file0" +%Y%m%d%H%M.%S)
 
1 members found this post helpful.
  


Reply

Tags
bash, code, script, tasks


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Shell Script: "bash: Bad Substitution - Script for remove "." " thiagofw Programming 14 12-09-2016 10:04 PM
[SOLVED] Bash - Capture "Child" script output with "Parent" without using files or coproc keyword Jason_25 Programming 2 02-14-2016 07:51 PM
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
[SOLVED] troubles with bash script tests "-z" and "-n" SaintDanBert Linux - Software 7 04-10-2012 09:26 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:23 PM.

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