sh

Bash convert string to timestamp

孤人 提交于 2020-01-03 02:25:23
问题 I have a string in format 20141225093000 which represents Dec 25, 2014 09:30:00 and I want to convert the original format to a unix timestamp format so i can do time operations on it.How would I do this in bash? I can easily parse out the values with expr but I was hoping to be able to identify a format like YYYYmmddHHMMSS and then convert it based on that. 回答1: With GNU date, you can convert YYYY-MM-DDTHH:MM:SS to epoch time (seconds since 1-1-1970) easily, like so: date -d '2014-12-25T09:30

get variables substituted when I cat a file

倾然丶 夕夏残阳落幕 提交于 2020-01-02 07:35:25
问题 Is it possible, in a clean way to get the variable values when I cat a file, instead of the variable names, as written in the file. It's hard to explain, but here goes a simple example: $ cat <<EOF $HOME EOF /home/myself cat returns /home/myself because it is already expanded by the shell. $ echo \$HOME >/tmp/home $ cat /tmp/home $HOME cat simply reads the file, I want $HOME to be expanded here somehow by cat, because the file will contain variable names (not like HOME=/home/myself) My

How to change current working directory inside command_not_found_handle

十年热恋 提交于 2020-01-02 05:20:47
问题 I'm trying to write a not found handle in Bash that does the following: If $1 exists and it's a directory, cd into it. If $1 exists inside a user defined directory $DEV_DIR , `cd into it. If the previous conditions don't apply, fail. Right now I have something like this: export DEV_DIR=/Users/federico/programacion/ function command_not_found_handle () { if [ -d $1 ]; then # the dir exists in '.' cd $1 else to=$DEV_DIR$1 if [ -d $to ]; then cd $to echo `pwd` else echo "${1}: command not found"

1: command not found

不问归期 提交于 2020-01-01 15:36:13
问题 I'm writing a divides-by-three function in Bash, and it won't let me set a variable to a number. fizzy.sh: #!/usr/bin/env sh div3() { return `$1 % 3 -eq 0` } d=div3 1 echo $d Example: $ ./fizzy.sh ./fizzy.sh: line 7: 1: command not found 回答1: Bash functions normally "return" values by printing them to standard output, where the caller can capture them using `func args ...` or $(func args ...) This makes functions work like external commands. The return statement, on the other hand, sets the

How do I overwrite multiple lines in a shell script?

旧街凉风 提交于 2020-01-01 00:39:08
问题 I want to write multiple lines over and over to the terminal. Something like echo "One Line" echo "Two Lines" echo "\r\b\rThree Lines" echo "Four Lines" Ideally this would first output: One Line Two Lines And this output would then be replaced with Three Lines Four Lines Trouble is, while the carriage return will let you overwrite one line of output, you can't get past the \n with a \b. How do I then overwrite multiple lines? 回答1: I found a solution for this one that took a little digging and

Bash script - “tar czf …” command ignore parameters

被刻印的时光 ゝ 提交于 2019-12-31 06:12:12
问题 I have a simple bash script called script.sh !#/bin/bash tar czf /var/log/apache2/Backup_$(date "+%d-%m-%Y").tar.gz /var/log/apache2/ --exclude='backup*' --exclude='Backup*' When I rund this line in SSH-Console, all works fine but when I run the .sh-script at linux console with bash ./script.sh then the tar czf ... command works but ignore the --exclude parameters Whats wrong? I don't know... :/ 回答1: you have to use : !#/bin/bash tar -czf /var/log/apache2/Backup_`date "+%d-%m-%Y"`.tar.gz /var

Get date of last saturday - BusyBox 1.1.0

独自空忆成欢 提交于 2019-12-31 05:15:12
问题 Since the date in BusyBox is not as powerful as gnu date , I have problems to calculate the date of last saturday. last_sat=`date +"%Y-%m-%d" -d "last saturday"` only works fine with gnu date. I've found something like this to calculate from Epoch busybox date -D '%s' -d "$(( `busybox date +%s`+3*60 ))" but my BusyBox (v1.1.0) doesn't recognize the -D argument. Any suggestions? 回答1: For the last Saturday before today, under busybox 1.16: date -d "UTC 1970-01-01 $(date +"%s - 86400 - %w *

rsync in shell for loop

喜你入骨 提交于 2019-12-31 03:31:07
问题 I have this shell script #!/bin/sh PATHS=( a b c d ) for PATH in ${PATHS[@]} do rsync -avziP /home/user/$PATH $SERVER:$server_folder -b --backup-dir=$backup_folder/backup_$date --delete --exclude=.* --log-file=$HOME/rsync.log done And I always get this error: rsync: command not found What is driving me crazy is that if I delete the for loop, and just run the rsync command, the script works perfectly 回答1: PATH is a reserved variable! It is the variable specifying where to search tools (like

Why is '$_' the same as $ARGV in a Perl one-liner?

a 夏天 提交于 2019-12-31 01:54:14
问题 I ran into this problem while trying to print single quotes in a Perl one-liner. I eventually figured out you have to escape them with '\'' . Here's some code to illustrate my question. Let's start with printing a text file. perl -ne 'chomp; print "$_\n"' shortlist.txt red orange yellow green blue Now let's print the name of the file instead for each line. perl -ne 'chomp; print "$ARGV\n"' shortlist.txt shortlist.txt shortlist.txt shortlist.txt shortlist.txt shortlist.txt Then we can add

What is the use of “echo || true”?

孤街浪徒 提交于 2019-12-30 06:16:45
问题 Why would anyone want to have a code like echo "something" || true ? I found such a usage on line 92 of /lib/lsb/init-functions on an Ubuntu 14.04: echo "$pid" || true I understand that || stands for OR. But I could not find a way to make echo produce any sort of exit code other than zero. And even if it does, who cares? I mean, using the prefix || true ensures that the exit code is always 0. So if the left-hand side of the double pipe yields anything different from 0, true will be executed