Tag Info

New answers tagged

0

If I understand correctly, you have directories that contains files called 1, 2, 3, etc. (up to a variable limit) and you want to replace each directory by a file which has the same name as the directory, and is the concatenation of the files in numerical order. In this answer, I'm going to use zsh, because it's a lot easier to use than a combination of ...


1

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 ...


1

You basically want dstat to provide you with an average over a given time (say 120 secs). This works exactly the same as vmstat or ifstat, the first argument is the delay, which means the given time to provide averages over. Default is 1 second. So this should do: dstat -n 120 or for given interfaces eth0 and eth1, do: dstat -n -N eth0,eth1 120 or for ...


0

With find and sed: find -type f -exec sh -c '[[ $(sed -n $= $0) -eq 2 ]]' {} \; -exec sed -i '1!d' {} + Note: find should be tweaked with PATH and -maxdepth depending on the directory structure.


1

try this script it works fine. for file in $(ls *.txt) do LINENUMB=`wc -l $file | cut -f1 -d' '` if [[ $LINENUMB == 2 ]]; then sed -i '$d' $file fi done


0

I go to my proposal assumes that your files are numbered, e.g. foo1, foo2, ... foo8). Run this command in a terminal: wc -l foo[1-8] | head -n -1 | awk '{print ($1==2)?system("sed -i \"$ d\" " $2):$2 " has not exactly 2 lines"}'


0

Short answer: no. Long answer: you can iterate over your keys (the output of KEYS - do not use that command in production! Use SCAN instead), call for each key the OBJECT IDLETIME command and delete based on the response. Longer answer: you can actually change scrumplr's source to have Redis automatically expire keys after 30 days. The suspect file appears ...


0

When you do not want to grep the info directly out of the logfile, you can use echo "${var}" | grep "kdump" You could also use a pipe: awk ...your_script | grep "kdump" When you want to grep more words, use grep -E awk ...your_script | grep -E "kdump|Diggy|other string"


-1

Another alternative is to use the yes command in your script. yes newpassword | passwd youruser This will send newpassword to the passwd command for youruser. It should be mentioned that setting/modifying user passwords via scripts may present security risks and should be avoided whenever possible. EDIT: This answer requires root access. Apologies for ...


4

You're looking for the chpasswd command. You'd do something like this: echo 'pi:newpassword' | chpasswd # change user pi password to newpassword Note that it needs to be run as root, at least with the default PAM configuration. But presumably run as root isn't a problem for a system deployment script. Also, you can do multiple users at once by feeding it ...


0

When you call your script you need to enclose the given string. Say your script is called parse.sh then you should call it using: ./parse.sh "test & test" or ./parse.sh 'test & test" If you call the script using: ./parse.sh test & test the shell will try to execute ./parse.sh test in the background and run test as next command. See ...


0

It doesn't seem that detox has an option for that. It should be fairly simple to modify the source code to add a filter with your desired output (a small modification of the safe filter; don't forget to ensure that any leading - gets removed). You could postprocess the result of detox, or use other tools altogether. There are many file renaming tools that ...


1

Using (gnu)grep: grep -m1 -oP '(?<=class=val>).*?(?=</td>)' grep -m1 -oP 'class=val>\s*\K[0-9.]*' # \cite{Costas)


1

This is a very wide question involving several tools... First, to connect a Windows Share, you will need mount.cifs command. It's available from cifs-utils package (the name of the package may vary depending on distro). A simple example of its use: mount.cifs //10.1.1.10/RAJ /mnt/windows -o rw,username=Swapnil man mount.cifs to see all available options, ...


1

GNU date accepts a number of relative dates that you can supply with the -d flag. $ date -d '+1year' Sun Apr 17 09:15:14 PDT 2016 See Relative items in date strings for details.


1

If you want to let date create the standard date formatting you could do: $ date -d "$( date -d "Fri Apr 17 20:16:01 IST 2015" "+2016-%m-%d %T" )" But you can also use just one date instance and use the appropriate time format specifiers, as in: date -d "Fri Apr 17 20:16:01 IST 2015" "+%a %b %e %T %Z 2016" Or - since your date format has the year at ...


2

If you want to use getopts (noted the "s") to get the command line arguments you can do something like while getopts "i:n:e:" OPT; do case "$OPT" in i) # do stuff with the i option ID="$OPTARG" ;; n) # do stuff with the n option ;; e) # do stuff with the e ...


0

This is quite basic. You can pass the name as an argument. #!/bin/bash UNIXID=$1 ldapsearch -x "(cn=$UNIXID)" | awk '/givenName/||/mobile/||/mail/' ##################### #./lsearch in15004 givenName: Mr. Xyz mail: x@abc.com mobile: 9xxxxxxxx1


0

Instead of getopts you can also simply use the built-in Bash variables $1, $2 etc. inside your script. These variables are automatically assigned to the first, second etc. argument passed to the script. So if your program is run with ./lsearch –i in15004, inside the script $1 will take the value -i and $2 the value in15004. The name of the script, in this ...


8

The function-based approach results in the clearest code. There's at least two variants possible, the one suggested by FloHimself above in the comment (which would not be my preferred way): function myprint { if type banner >/dev/null then banner "$@" else echo "$@" fi } myprint "Hello World!" or - the variant I'd prefer - to use a ...


2

The simplest way is to just run command1 "hello world" || command2 "hello world" If the first command doesn't exist, the left hand side of the || wil fail so the command on the right will be run. I don't see why you need to test first. Just do, and if you fail, do something else. You can make that slightly better by ignoring error messages cause by a ...


3

Inter process communication isn't a small question. There is a lot of "it depends" in there. Perl has a whole chapter on the subject under perlipc However for your scenario as described - I'd write a state file, flock it and then write your data there. The flock ensures you don't have a race condition, and the file can be of arbitrary length. You can use ...


0

On another server I was getting awk: line 2: function strftime never defined and there is no easy way to install gawk so I figured out how to do it without awk just with date (as per @Hrvoje Špoljar's hint) ping -D localhost | while read row ; do if [[ $row == \[*\]* ]]; then echo -n \[$(date -d "@$(echo $row| sed 's/^\[//' | sed 's/\].*//')")\] ; fi ; ...


0

You might want to investigate mosh which is like ssh modified for unreliable connections. From the website: Mosh (mobile shell) Remote terminal application that allows roaming, supports intermittent connectivity, and provides intelligent local echo and line editing of user keystrokes. Mosh is a replacement for SSH. It's more robust and ...


0

This one is a bit simpler to use and set-up, named args, etc. https://github.com/uudruid74/bashTheObjects


0

I don't know the BBB but as said heemayl you can use screen (or tmux which I find more user friendly). Those tools allows you to connect to a remote machine, do what you want (starting a script for example), detach the session and reconnect to it later. The advantage of tmux is that it allows to create easily several sessions on the machine and slice them ...


0

This may work for you: #!/bin/bash # cd /a || exit find . -type f | while read FILE do TARGET="/app/$FILE" test -f "$TARGET" && cp -fp "$TARGET" "$TARGET.bak" mv "$FILE" "$TARGET" done


0

You can try this: #!/usr/bin/env ksh a_files=( $(find /path/to/a/ -type f) ) for file in "${a_files[@]}"; do mv "/path/to/app${file##*a}" "/path/to/app${file##*a}.bak" mv "$file" "/path/to/app${file##*a}" done If a/ and app/ are in same directory e.g. /home/user/{a,app}, then: #!/usr/bin/env ksh a_files=( $(find /home/user/a/ -type f) ) ...


3

You can have some kind of watchdog script running that requires a signal via the remote connection every n (milli)seconds and have it do something when not receiving the signal. See How to introduce timeout for shell scripting? for an example of an expect-script that prints a message on timeout; you can alter it to create a safe shutdown. The watchdog echo ...


1

Use the find command to enumerate files in a directory and its subdirectories recursively. There are several ways to rename files. You can use the Linux rename command: find Music -depth -name '*_000' -exec rename _000 '' {} \; This removes the first occurrence of _000 in each file name. Beware that if you had files whose name contains _000 in the middle, ...


2

Although mikesrv solution is much better and safer I think this can be done also with rename: find Music/ -type f -name '*_000' -print0 | xargs -r0 rename -v '_000' '' But there are still potential problems as mentioned, for example album_00020_000 will be renamed to album20 which obviously is not the desired behaviour. I think on debian distribution was ...


2

cd Music/.. && mkdir Music2 && pax -'rwls|\([^/]\)_000$|\1|p' Music Music2 If run from Music's parent directory, the above commands will create a directory called Music2 then mirror Music's path structure in Music2 w/ hardlinks while removing any occurrence of _000 found at the tail of any filename therein. Afterward you'll have ...


2

You should not believe them if they tell you it cannot be done. You should believe them, however, if they tell you it's not easy. sed '\|*/|!{ s|/\*|\n&| #if ! */ repl 1st /* w/ \n/* h; s|foo|bar|g;/\n/!b #hold; repl all foo/bar; if ! \n branch G; s|\n.*\n||;:n #Get; clear difference; :new label n; ...


3

This seems to work: #!/bin/sh pressed_ctrl_c= trap "pressed_ctrl_c=1" INT while true do (trap "" INT; foo)& wait || wait if [ "$pressed_ctrl_c" ] then # echo break fi done Initialize pressed_ctrl_c to null.  This is probably not necessary in a script. trap command signum tells ...


-1

I tried that as well, the situation I am having is I have 7 applications running on this server and each app uses a different IP, so what I am doing is if the app is app4 then do /sbin/ip addr ls eth0 | awk '/inet / {print $2, $8}' 133.8.5.9/16 eth0 133.8.5.8/16 eth0:1 133.8.5.7/16 eth0:2 133.8.5.6/16 eth0:3 133.8.5.5/16 eth0:4 133.8.5.4/16 eth0:5 ...


3

You could try this sort of construct: #!/bin/bash # INTR= trap 'INTR=yes; echo "** INTR **" >&2' INT while : do ( # Protect the subshell block trap '' INT # Protected code here echo -n "The date/time is: " sleep 2 date read -t2 -p 'Continue (y/n)? ' YN || echo test n = "$YN" ...


1

With perl -F: -anE 'if( (stat $F[5])[4] != $F[2] ) { say "$F[0]($F[2]) not own $F[5]" }' /etc/passwd you get almost what you wanted. In /etc/passwd: F0 = username F5 = home F2 = uid stat FileOrDir [4] is the uid of FileOrDir Add some more condition to tune it. Example: perl -F: -anE 'if( -d $F[5] and # F5 is a ...


2

Or your awk could be: awk -F: 'BEGIN { FS = ":"; nores = 1; } { if ((system("[ -d " $6 " ]") == 0) && ($7 != "/sbin/nologin")) { "stat -c \"%U\" " $6 | getline s; if (s != $1) { print "The directory " $6 " exists for user " $1 " but is not owned by that user"; nores = 0 } } } END { if (nores) print "No results"; }' /etc/passwd


3

flag=0 testuser=$(stat "/home/testuser" -c %U) while IFS=':' read -r myuser a b c d mydir e do if [ -d "$mydir" -a "$e" != "/sbin/nologin" ] then if [ "$myuser" != "$testuser" -a \ "$myuser" != $(stat "$mydir" -c %U) ] then echo "The directory $mydir exists for user $myuser" \ "but is not ...


3

You could use a BEGIN block to set a flag unset the flag when your current test finds a missing directory check to see if the flag is still set in a END block and if so print your message e.g. awk -F: ' BEGIN{nores=1;} {if(system( "[ -d " $6 " ]") == 1 && $7 != "/sbin/nologin" ) {print "The directory " $6 " does not exist for user " $1; nores=0 ...


1

This code does the comparison and informs you by printing a message on the console where you typed the command: [[ $(date +%Y%m%d -d $(< file)) == $(date +%Y%m%d) ]] && echo "wake up" You may want to replace the echo by any other command that notifies you, e.g., use the command mail.


2

It is the back tick quotes. They tell the shell to run the output of the command. e.g. `echo ls` will run ls. In your case you have asked bash to run the sql files. This is obviously not what you intended, as bash can not do this, the sql will not make sense to bash. Also, even with this fix, the script will not do what you describe. Someone else ...


1

To "loop through a bunch of [.sql] files and get their names" you'd just do: for f in *.sql do do_whatever_with_file "$f" done If you just want to list the files: ls *.sql If you want to test whether there are *.sql files existing, for example: if ls *.sql >/dev/null 2>&1 then echo sql files existing else echo no sql files fi


0

If the elements of your exclude list don't contain any whitespace or any wildcards, you can define a ROOT_EXCLUDE variable and use it unquoted. Unquoted variable expansions split the value of the variable at each whitespace sequence¹; then each element is expanded as a wildcard pattern if it contains any wildcard. ROOT_EXCLUDE="--exclude=/dev ...


2

var=$(awk '/eth0:4/ {print $2}' file)"


0

Inside the for-loop, after the mkdir, you can add this code: if [ -d "${target}/${i}" ]; then PASS=$(($PASS + 1)) else FAIL=$(($FAIL + 1)) fi Before the for-loop, initialise PASS and FAIL to 0. After the for-loop, simply echo PASS=$PASS FAIL=$FAIL.


2

As mentioned in comment you can use: read -t 1 -n 1 key which because of -t option we can remove sleep, so your script could be: #!/bin/bash for ((i=0; i<100; i++)); do read -t 1 -n 1 key if [ "$key" = "k" ]; then i=$((i + 10)) fi echo $i done But I think more portable could be: #!/bin/bash if [ -t 0 ]; then stty -echo ...


0

Instead of using commands like ifconfig -s or netstat -i one can check the following files to monitor a wi-fi connection status: /sys/class/net/wlan0/flags indicates wheter the device is on or off. It tells nothing about packet transmission so it's not really useful to check if your connection is working or not. It's contents/values are 0x1003 (on) and ...


0

#!/bin/bash cd /home/Downloads/complete for dir in */; do cd "$dir" output=$(rhash --check *.sfv | tail -1) cd .. if [ "$output" == "Everything OK" ] then mv "$dir" ../complete+crcok fi done


3

You know, I'm not convinced you necessarily need a repetitive feedback loop as your diagrams portray, so much as maybe you could use a persistent pipeline between coprocesses. Then again, it may be there isn't too much of a difference - once you open a line on a coprocess you can implement typical style loops just writing information to and reading ...



Top 50 recent answers are included