Tag Info

New answers tagged

0

I figured it out. While I am not sure how the issue with the permission thing is, I can instead shoot first and ask later like this: sudo losetup -f myfile.img ld=$(losetup -j myfile.img | grep -o "/dev/loop[0-9]*") dostuffwith $ld


0

The first problem is that you have a space after the =: TEMP= grep -o [1-9][0-9]\.[0-9] /tmp/temp.txt That's wrong and breaks your script. The next issue is that you're assigning the commands themselves, as strings, to the variables and not their output: $ foo=date $ echo $foo date To run the command and save its output to a variable, you need to use ...


0

Assuming vcgencmd measure_temp will create a single fixed point number, this command may do what you want: temp=$( vcgencmd measure_temp | grep -o '[1-9]*\.[0-9]' ) date "+%Y-%m-%d %H:%M;${temp}" >> /home/pi/temp_hist.csv


0

Instead of building a string and appending to it, just create the string directly: ➜ ~ DATE="1234" ➜ ~ TEMP="15" ➜ ~ STR="$DATE;$TEMP" ➜ ~ echo $STR 1234;15


3

I like Cyrus's answer, but this syntax also works: #!/usr/bin/env bash fail_color=$'\033[31;1m' color_end=$'\033[0m' function="foo" line_number="42" printf "%sError - Function: %s, Line: %d%s\n" "$fail_color" "$function" "$line_number" "$color_end" And ShellCheck says "It all looks good!". :)


6

fail_color="\033[31;1m" color_end="\033[0m" function="foo" line_number="42" printf "%bError - Function: %s, Line: %d%b\n" "$fail_color" "$function" "$line_number" "$color_end" Output: Error - Function: foo, Line: 42 Tested with Ubuntu 11.04 (bash 4.2.8(1)-release), Ubuntu 14.04.1 LTS (bash 4.3.11(1)-release), RHEL 5.1 (bash 3.1.17(1)-release), RHEL ...


0

There are many ways of doing this. If you just want to list the files, you can use ls: ls a_date\(s\)/*csv Or, with find: find .. -path '*a_date*/*csv' find ../allCSVs/a_date\(s\)/ -name '*csv'


-1

The variable "$fail_color" contains one too many, or one too few backslashes before the 033, try changing it or removing the "" to have it debackslashified. \033 is supposed to be the ESC character (ASCII 27 decimal, 033 octal).


0

You could use Recoll that helps you to search any document type and also restrict your search to a particular folder as shown below. Simple Search Advanced Search(To restrict search to a particular folder)


2

An external linting tool like ShellCheck can detect problems and may have better messages and locations than bash itself. For a program that is mostly echo statements with a "${MY_ARRAY[1]" in the middle, ShellCheck tells you that it couldn't parse the quoted string. It even pins the problem down to the $ character and hints that the parameter expansion was ...


0

You probably should use a batch tool for batch operations. Doing so will usually entail reading/writing all records in a single stream rather than, for example, invoking a separate cp process for each file copied. There is already a cpio answer written here, which, given only the options already provided you is likely what I would choose. However, the cpio ...


2

A low-tech approach is tr -cd "'\n" < run_me.sh | awk 'length%2==1 {print NR, $0}' The tr deletes all characters except for single quotes and newlines, and the awk identifies the lines that have an odd numbers of characters (i.e., non-matching quotes).  Check them out individually; note that valid strings like "That's not a bug!" will be flagged.  If ...


0

You're using Bash-like syntax and operators ($(command), ${...//}), but running it by sh, which on Android is not Bash by default.


1

There's nothing wrong with using a temporary variable: variables are made to store data to be used several times. In this case, you can combine the two grep/sed calls into a single call to GNU grep, sed or awk. It's easy with sed: pass the -n option to output only explicitly printed lines, and print the lines on which a substitution is made. echo "$( ...


4

Easiest way? Use an editor with syntax coloring that's aware of the shell you're using, and inspect visually. When you get a big swathe of string color, you know you left out a quote. Just about any decent programming editor has syntax coloring. Syntax coloring sometimes gets it wrong, but that's usually a sign that your program is too complex and humans ...


1

I think maybe this issue is very the same as yours, you also can check this out: Create sub-directories and organize files by date I write this new script based on that issue's first answer: for x in *; do d=$(date -r "$x" +%Y) mkdir -p "/your/new/directory/$d" mv -- "$x" "/your/new/directory/$d/" done write this script to a file named copy.sh in ...


4

If you have newer files on the old disk that you want to ignore I would go about it like this Create a temporary marker file with a modified-by date that separates files I want from those I don't Copy files older than the marker file to the new location Here are sample commands for this, which assume you want to maintain any directory hierarchy from the ...


4

Do not use ls. It's not recommended to use in such cases. Moreover using grep to filter according to date is not a good idea. You filename might itself contain 2012 string, even though it was not modified in 2012. Use find command and pipe its output. find . -newermt 20120101 -not -newermt 20130101 -print0 | xargs -0 cp -t /your/target/directory Here, ...


1

For the record, with zsh, there's the ${^array} operator that turns on brace-like expansion on the elements of the array. So: $ a=(one two three) $ b=('foo '${^a}' bar') $ printf '<%s>\n' $b <foo one bar> <foo two bar> <foo three bar> Search and replace also works: $ printf '<%s>\n' ${a//(#m)*/foo $MATCH bar} <foo one ...


0

You have two options here You can create a file with all the servers' IP addresses, then do the below while read -r ip;do ssh-copy-id -i .ssh/id_rsa.pub $ip done < servers.txt Assuming servers.txt is the file with IPs/hostnames You can put all your IPs/hostnames in a loop, and run ssh-copy-id like below for i in hostname1 hostname2 do ...


2

Simply you can compare the performance of these operators with time command: time [ 1 -eq 0 ] real 0m0.000s user 0m0.000s sys 0m0.000s time [ 1 = 0 ] real 0m0.000s user 0m0.000s sys 0m0.000s Which is real means Wall Clock time and user means User Space time and sys means System or kernel time. Now, if you compare these operators with ...


2

p='* "foo ' s=' bar $USER' CATEGORIES=(one two three four) CATEGORIES=("${CATEGORIES[@]/#/$p}") CATEGORIES=("${CATEGORIES[@]/%/$s}") paste <(printf '[%s]\n' "${!CATEGORIES[@]}") \ <(printf '%s\n' "${CATEGORIES[@]}") Output: [0] * "foo one bar $USER [1] * "foo two bar $USER [2] * "foo three bar $USER [3] * "foo four bar $USER ...


1

Here's a one liner of the iw command that have the same output as you. iw dev wlp1s0 link | grep 'SSID:\|signal' | awk '{printf "%s ", $2$3}' My output: ZyXEL-AP-2,4GHz -46dBm


0

Something like that ? iw dev wlp1s0 link | grep -E '^\s*(SSID|signal):\s' | sed -r 's/^\s*(SSID|signal):\s//' | awk '{printf $0}' grep will accept (SSID|signal): so it will match both SSID: and signal:. -E is optional, but if you don't use it, think about escaping special meaning character. Here, it would be \(SSID\|signal\): The same regex can be used ...


0

You could use awk: echo $( iw dev wlp1s0 | awk '/(SSID|signal): /{$1 = ""; print;}' )


3

Depending on what your ultimate aim is, you could use printf: $ a=(1 2 3) $ printf "foo %s bar\n" "${a[@]}" foo 1 bar foo 2 bar foo 3 bar printf re-uses the format string until all the arguments are used up, so it provides an easy way to apply some formatting to a set of strings.


0

You can get the process ID of process by running it as a background process. ./process1 & pid1=$! wait "$pid1" The wait command waits for process1 to exit (but not its subprocesses), like in your original script. Do note that in your original script, there's no process1 to kill at the end: the ./process1 command finishes only when process1 exits. It's ...


0

Your script will have to be able to access the password. You can't use a hash of the password: if you did, the hash would be the password, since that would mean anyone with the hash could log in. You should put the password in a separate file, and read it from your script. Take care to reproduce the password exactly (for example, don't use echo -e "$PASS" ...


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

You don't need test command when using case, and don't need case when using test: case $1 in ("") echo "something" ;; esac and: [[ -z $1 ]] && echo "something" or using old test [...] for portability: [ -z "$1" ] && echo "something"


1

According the manual (man timeout): Synopsis timeout [OPTION] NUMBER[SUFFIX] COMMAND [ARG]... [...] If the command times out, then exit with status 124. Otherwise, exit with the status of COMMAND Combine this with the knowledge that the exit status or return value is stored in the variable, $?, and we have... timeout 5 ...


3

If timeout times out, it exits with status 124; you can check this to determine whether the script timed out or not.


0

pkill can kill processes by name: pkill process1 && pkill sub_process1 should do the job.


0

What you could do is kill -TERM you process group. If your script is invoked from a shell, it'll have it's own process group equal to the script's PID: kill -TERM -$$ If it is invoked otherwise, it's process group id might not be equal to its PID: kill -TERM -`ps -o pgid $$ | tail -1` You shouldn't kill -9 in a script or a program unless you give the ...


1

Why not use top -bd 5 -n 3 -p `pidof init` >> /home/test/nik/myscriptoutput2 instead of your whole script... In PHP you can use <?php $file = "/home/test/nik/myscriptoutput2"; $output = shell_exec('top -bd 5 -n 3 -p `pidof init`'); file_put_contents($file, $output); ?>


1

Since you tagged this as Linux: pgrep / pkill to the rescue: PID_OF_SUB_PROCESS1=$( pgrep -P $PID_OF_PROCESS1 ) pkill -P $PID_OF_PROCESS1


4

ClusterSSH gives you a window on each machine and with a common window to control all windows. If we are talking 10 machines this will work. If we are talking 100 machines, there will be to many windows. The beauty of ClusterSSH is that if one machine is not 100% like the rest, you can just click the window, and send keystrokes only to that machine before ...


0

You could also try to use the device by the USB address (i.e. controller/hub and device number), like so: ls -l /dev/bus/usb/0??/0?? You can find out the device number by using the lsusb command (try using it as root if you do not get all the information you need, albeit it usually tends to give you rather a lot of info), or use udevadm info ...


1

I had a similar problem with a USB device using the pl2303 driver. Every now and again /dev/ttyUSB0 would vanish and /dev/ttyUSB1 would appear in its place. I solved it by running a cronjob every 15 minutes that did the following: if [ ! -c /dev/ttyUSB0 ] then echo `date` Device missing echo `date` Stopping zmconcopy `/usr/sbin/rczmconcopy stop ...


0

In order to simplify the problem and since your are getting the variable sitename, why don't you read a username variable? With that you'd make sure that the script execution is not dependent on the environmental variables made available the way the script is executed.


0

If, for some reason, $USER is not set, you can use the id command to obtain the identity of the real user. So the first time you use the $USER variable, you can use the shell expansion to supply a default value. Change the chown line in your script to: sudo chown ${USER:=$(/usr/bin/id -run)}:$USER /var/www/$sitename If USER is empty or unset when this ...


5

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


0

With Perl and bash: decoded=$( perl -lpe 'y/+/ /; s/%([0-9a-f]{2})/chr(hex $1)/egio' <<<"$str" )


19

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


0

Here is a proper answer using xmlstarlet. This is a tool used for xml parsing and editing. First of all, install this package on your system. If you're on a Debian-based system, then do: sudo apt-get install xmlstarlet Now, first we read the value of base64 encoded string then we decode this string then we modify the corresponding tag value Here is ...


1

Closest you'll get is with ps aux. This will show you the number of virtual pages allocated to a program. Whether these pages are actually used, you won't know unless you profile every running process. These numbers should be good enough for finding a leak, though


4

#!/bin/sh - urldecode() { python -c "import sys, urllib as ul;print ul.unquote_plus(sys.argv[1])" "$1" } str="this+is+%2F+%2B+%2C+.+url+%23%24coded" decoded=$(urldecode "$str"} printf '%s\n' "$decoded" That is: avoid aliases in scripts as that's not guaranteed to work (some sh implementations like bash ignore aliases when non-interactive) quote your ...


1

I stole this from drupal.org, but you could do something like this: while [ $# -gt 0 ]; do case "$1" in --p_out=*) p_out="${1#*=}" ;; --arg_1=*) arg_1="${1#*=}" ;; *) printf "***************************\n" printf "* Error: Invalid argument.*\n" printf "***************************\n" exit 1 esac ...


0

Try something like wget -qO- http://www.google.com/ | tr " " \\n | fgrep src= | tr \" \\n | fgrep -v src= which will, however, output URLs as they are written in the file - which may be relative the the base URL.


5

If your script expects one prompt answered, or several prompts in which you can give the same answer, there's yes: NAME yes - output a string repeatedly until killed SYNOPSIS yes [STRING]... yes OPTION DESCRIPTION Repeatedly output a line with all specified STRING(s), or `y'. Use it like this: yes Me | give_a_hug.sh



Top 50 recent answers are included