bash

Re asking for a variable after execution in bash (like an alternative to goto) [closed]

隐身守侯 提交于 2021-02-05 12:05:17
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 days ago . Improve this question I'm reformulating a bit better my previous question from today. So basically, I have a bash script where i have variable=$(zenity list dialog) if test $variable = "something i've checked in zenity" then run command & restart the script elif... ...another command, etc... fi

Run awk comand on a remote server through ssh

血红的双手。 提交于 2021-02-05 11:46:45
问题 I'm trying to get a specific set of values from a file on a remote server. The command works fine when executing that through terminal. First ssh Command sshpass -p password ssh -T user@ip Second Awk Command find /opt/Info_Source/*daily* -type f -mtime -1 -exec zcat {} \; 2>/dev/null | awk -F, -v OFS=',' '$5 ~ /Valid/ && length($2) {print $2}' but if I combine both of them in a script #!/bin/ksh emp_id=`sshpass -p password ssh -T user@ip -q << EOF find /opt/Info_Source/*daily* -type f -mtime

convert JSON array to bash array preserving whitespaces

旧城冷巷雨未停 提交于 2021-02-05 11:39:53
问题 I want to transform JSON file into bash array of strings that i will later be able to iterate over. My JSON structure is as follows: [ { "USERID": "TMCCP", "CREATED_DATE": "31/01/2020 17:52" }, { "USERID": "TMCCP", "CREATED_DATE": "31/01/2020 17:52" } ] And this is my bash script: test_cases=($(jq -c '.[]' data.json)) echo ${test_cases[0]} echo ${test_cases[1]} echo ${test_cases[2]} echo ${test_cases[3]} As you can see it returns array with 4 elements instead of 2. Output: {"USERID":"TMCCP",

Escape single quotes in shell script

寵の児 提交于 2021-02-05 11:28:40
问题 I need to escape single quotes in a variable. ssh_command 'file=$(hostname)_server-setup_$(date +%Y-%m-%d).tar.gz && cd /var && tar -zcvf $file ini | wc -l | xargs printf \'Num files: %d, File: $file\'' I can surround the variable with double quotes but then the internal variables will be evaluated when the variable is declared, and that's not what I want. ssh_command "file=$(hostname)_server-setup_$(date +%Y-%m-%d).tar.gz && cd /var && tar -zcvf $file ini | wc -l | xargs printf 'Num files:

Creating a git alias containing bash command substitution and an argument

安稳与你 提交于 2021-02-05 11:27:07
问题 Got this: git diff $(git status -s | awk 'FNR == $LINE_NUM {print $2}') ...where line number is the file output by status. Now I want to create an alias for this command in .gitconfig : [alias] diff-num = $WHAT_SHOULD_I_PUT_HERE? I want this alias to run git's diff command based on the line number argument I'll type in on the command line. I'm new to bash and I'm not sure if I need a bash function to handle this or how to pass the argument into the substituted command. 回答1: The usual form for

why subtraction return - symbol

懵懂的女人 提交于 2021-02-05 11:18:04
问题 I have a problem with a simple subtraction but I don't understand what's wrong. My code : start= date +%s%N | cut -b1-13 #Treatment... end= date +%s%N | cut -b1-13 delta=`expr $end - $start` echo "delta $delta" My console display : 1374652348283 ... 1374652349207 delta - My question is : Why do I got a - symbol returned ? 回答1: The command: a= b (note the space) will set a to an empty string while it runs the command b . It's a way to temporarily set environment variables for a single command,

BASH : automatic quoting of tilde (~) character

*爱你&永不变心* 提交于 2021-02-05 10:48:50
问题 Here is a small snippet to showcase the problem #/usr/bin/bash RSYNC=/usr/bin/rsync RSYNC_OPTIONS="-aq --backup --suffix=~ --backup=bkpdir --update" echo ${RSYNC_OPTIONS} DOCDIR="Documents" # Note : no trailing slash BKPDIR="Active-Backups" HOST=$(hostname) SRCDIR_DOC="~om/${DOCDIR}/" # Trailing slash added DESTDIR=$(readlink -f $(dirname "$0") )/${BKPDIR}/${HOST} echo "DESTDIR = ${DESTDIR}" echo "Backing up ${SRCDIR_DOC}" echo "${RSYNC} ${RSYNC_OPTIONS} ${SRCDIR_DOC} ${DESTDIR}/${DOCDIR}"

How can I just extract one underbar-separated field from a filename?

北慕城南 提交于 2021-02-05 10:48:07
问题 I have a list of file names like this: REG_2016120200hourly_d01_20161202_00_00_00.nc Of this name I would like to extract and put in a variable: 1)date 20161202 for file in /path/*; do filename=$(basename -- "$file") date=${filename:4:8} echo $date done and this is working, the script give me 20161202 and I don't know why 2)timestep 00 I need to take the firsts two zero 00 and I'm trying with timestep=${filename:34:36} but this doesn't work. I'm a little bit surprised because I used the same

BASH : automatic quoting of tilde (~) character

那年仲夏 提交于 2021-02-05 10:47:25
问题 Here is a small snippet to showcase the problem #/usr/bin/bash RSYNC=/usr/bin/rsync RSYNC_OPTIONS="-aq --backup --suffix=~ --backup=bkpdir --update" echo ${RSYNC_OPTIONS} DOCDIR="Documents" # Note : no trailing slash BKPDIR="Active-Backups" HOST=$(hostname) SRCDIR_DOC="~om/${DOCDIR}/" # Trailing slash added DESTDIR=$(readlink -f $(dirname "$0") )/${BKPDIR}/${HOST} echo "DESTDIR = ${DESTDIR}" echo "Backing up ${SRCDIR_DOC}" echo "${RSYNC} ${RSYNC_OPTIONS} ${SRCDIR_DOC} ${DESTDIR}/${DOCDIR}"

Bash while loop ends execution when “ssh” is in loop body

放肆的年华 提交于 2021-02-05 10:37:48
问题 Could anyone of you explain why the following bash while loop ends execution after the first iteration when ssh is executed in it's body, please? Input file : $ cat hosts 192.168.223.21 miner01 192.168.223.23 miner03 While without ssh - two iterations: $ while IFS=' ' read -r IP HOST; do echo "ip=$IP hostname=$HOST"; done < hosts ip=192.168.223.21 hostname=miner01 ip=192.168.223.23 hostname=miner03 $ While with ssh in body - one iteration : $ while IFS=' ' read -r IP HOST; do echo "ip=$IP