Linux - SoftwareThis 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
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.
#!/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.
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
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).
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.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.