Tag Info

Hot answers tagged

11

You could use the timeout command : if timeout 10 ping google.fr > /dev/null then echo "process successful" else echo "process killed" fi shows process killed, and if timeout 10 ls /usr/bin | wc -l > /dev/null then echo "process successful" else echo "process killed" fi shows process successful. Based on this, you could run each ...


10

Rather than type your password multiple times you can make use of pssh and its -A switch to prompt for it once, and then feed the password to all the servers in a list. NOTE: Using this method doesn't allow you to use ssh-copy-id, however, so you'll need to roll your own method for appending your SSH pub key file to your remote account's ...


6

Both are wrong with the zsh default option settings. You can easily see what's going on by using echo as the command instead of mv. Interactively, it looks like you have the null_glob option set. According to the zsh documentation that option is not set by default. What happens with that option unset depends on whether another option, nomatch, is set or ...


4

You can certainly kill the child process after some execution time and append the text file you need with '0' from within your bash-script. You may find Bash script that kills a child process after a given timeout useful.


4

If your concern is about aliases, just do: [[ $(unalias -- "$cmd"; type -- "$cmd") = *builtin ]] ($(...) create a subshell environment, so unalias is only in effect there). If you're also concerned about functions, also run command unset -f -- "$cmd" before type.


4

Alternative using xargs, sshpass and ssh-copy-id: Assuming your credentials living in credentials.txt in format user:password@server: $ cat credentials.txt root:insecure@192.168.0.1 foo:insecure@192.168.0.2 bar:realsecure@192.168.0.3 You could do: tr ':@' '\n' < credentials.txt \ | xargs -L3 sh -c 'sshpass -p $1 ssh-copy-id $0@$2' Note: ...


3

00-01 09 * * * /home/flexsys/test.sh (the job will start at 9:00 and run upto 9:01) That is not what this means. This means the job will run at 9am and run again at 9:01. Cron has no concept of killing jobs, only starting them. As for the while, you need to run the date command, find the number of minutes, and test if it is or isn't 00. On my system ...


3

The ./foobar.sh file is started by running it with whatever follows the #! in the first line. If this line reads #!/bin/sh -x then it would be identical to the sh -x foobar.sh case (assuming sh is resolved to /bin/sh from the PATH). Maybe it is not started by sh but bash? The -x flag prints debug info, i.e. every command before it is executed.


3

Using Ansible is fairly simple. Just replace <USER> with the real login name $ cd /path/to/public/key $ cat<<END > hosts host1.example.com 10.10.10.10 END $ ansible -i hosts all --ask-pass -u <USER> -m authorized_key -a "user=<USER> key='$(cat id_rsa.pub)'"


2

Most probably /etc/ppp/ip-up.d is the location you are looking for. My example is valid on Gentoo Linux but the same directory structure seems to exist on Arch. Every time a VPN connection is made /etc/ppp/ip-up is run, which typically executes /etc/ppp/ip-up.d/* in turn. Its first argument is the attached pppn device. Put this script under ...


2

Try to use pdsh. A lot of examples available on Project Page If you like to use simple bash script: #!/bin/bash HOSTS="host1 host2 host3" USER=root CMD="ls" for host in $HOST; do ssh ${USER}:{$host} "$CMD" done In all cases you would need to tune no-password auth using keys and append to ssh command: -i /path/to/key Example to use: #!/bin/bash ...


2

This should work: sed 's/.*[0-9]c[0-9].*//g' file.txt However as mentioned in the comment, above command will not remove the complete lines, so a better way would be to use the d command on matching lines instead: sed '/[0-9]c[0-9]/d' file.txt Or with extended regular expressions with GNU sed (-E is the preferred flag for compatibility with BSD* ...


2

sed '/match_string1/{ :1 N /\n.*match_string2/s/\n/; / t1 P D }' When script met line with match_string1 it add next line to pattern and check if in that added line there is match_string2 if so they substitute newline sign by ; and add next line to check. If there is not match_string2 (so substitution ...


2

A crude way to do it: for f in /path/to/PDFs/*.pdf; do base=$( basename "$f" .pdf ) if [ ! -f /path/to/PNGs/"$base".png ]; then mv "$f" /path/to/garbage/ fi done


2

It may be tempting to use eval, but as passwords can contain arbirary chars (which could/would be executed as code, it makes it risky; bordering on a plain-and-simple: "Don't do it!". This works - using arrays. Contents of test file User:blala Pass:blala with spaces: and colons: and $PATH IP:***.***.**.** set -f # ...


2

For filtering data from file is better to use grep. For example: grep <search string> <filename> With awk you can use something like: awk '/string/ {command}' <filename> To get IPs counted you can use uniq -c <filename> This will provide you uniq IPs with count


2

It depends on the content of the shell script and the used shell. A shell script might contain boilerplate code, called a shebang, like the following: #!/usr/bin/env bash The special sequence '#!' instructs the exec() kernel system call to use the program defined right after it as the interpreter. This means that you'll have to look into the file to see ...


2

It's correct in principle but you might consider reducing it to a single parted call. parted --script /device \ mklabel gpt \ mkpart primary 1MiB 100MiB \ mkpart primary 100MiB 200MiB \ ... Your alignment issue is probably because you use MB instead of MiB. You should not need an actual align-check command when creating partitions on MiB ...


2

You can use Linux netfilter to intercept incoming pings and send them to userspace. This will do it: iptables -I INPUT -p icmp --icmp-type echo-request -j NFLOG You can add any kind of iptables criteria like -s (source) in order to intercept only some pings and not others. Note that this does not cancel the kernel's handling of the original ping. It only ...


2

Windows line endings consist of the two-character sequence CR, LF. CR is the carriage return character, sometimes represented as \r, \015, ^M, etc. A Unix line ending is just the LF character. A way to convert Windows line endings to Unix line endings using only standard utilities present on all Unix variants is to use the tr utility. tr -d '\r' ...


2

And now, the systemd answer. You're using Ubuntu version 15. You have systemd. /etc/rc.local is at best a backwards compatibility mechanism in systemd. And as shown by the mess in the AskUbuntu question hyperlinked below, using it can go horribly wrong. So make a proper systemd service unit. You are creating a local, non-system non-package, service ...


2

You can simply add a line to call the script in /etc/rc.local. This file is the last of the init scripts to be run. Just make sure that /etc/rc.local is executable and owned by root.


1

You can add the line to the /etc/enviroment file like this: PATH=$PATH:~/root/scripts or Edit your ~/.bashrc and add your line here like this: export PATH=$PATH:~/root/scripts


1

You can combine all the actions in one command: sftp user@host:/path/to/file/$(tail -1 file1.txt |tr -s ' ' |cut -d ' ' -f 9) This will fetch the file into the current working directory. If you need to fetch the file into another directory specify the destination directory as a next argument to the sftp command.


1

00 09 * * * /home/flexsys/test.sh #!/bin/tcsh cd /home/A/B/C/ @ a = 0 while ( $a != 12 ) sleep 5 touch ABC.txt @ a = $a + 1 end This will run your script only once, but it will touch your file 12 times with a 5 seconds pause between each touch. As long as 12*5 is 60, you'll have ...


1

Use grep with PCRE: $ var='/home/path/archive/logs/path.log-2015-04-13.0.gz:2015-05-13 00:43:49,779 INFO [DEUX-DR-SAMPLE-1] c.i.s.p.DeuxProxyPMMProcessor [DEUX : 361] SVRREQ|dataID|server request: (deliver: (pdu: 0 5 0 282190) (addr: 1 1 adress) (addr: 1 1 mssidn) (sm: enc: ASCII msg: id:dataID stat:pattern)' $ grep -Po '.*?SVRREQ\|\K[^|]+(?=\|)' ...


1

dos2unix: sed -i -r -e 's/\r$//' file unix2dos: sed -i -r -e 's/$/\r/' file


1

An alias will work. I have grep aliased to grep --color=auto: % which grep grep: aliased to grep --color=auto And piping to grep has the behavior you desire:


1

I take it that "rolls up" means that all newlines are removed and the output of each command is thus "rolled up" to a single line. If so, your grep ... | awk ... will work, but you don't need both commands ("never use two when one will suffice" is generally a good idea). In addition, your command line has a couple issues (like no input for grep but a ...


1

The first version, with an explicit call to sh will run your script with sh (displaying trace statements, as requested by -x). The second one, which does not specify an explicit interpreter, will respect the shebang line, if any, or default to the shell you are currently running. For example, if your script has a shebang line of #! /bin/bash, or your shell ...



Only top voted, non community-wiki answers of a minimum length are eligible