Tag Info

New answers tagged

0

According to http://search.cpan.org/~nalobin/Net-Whois-Raw-2.85/lib/Net/Whois/Raw.pm, you can: set_ips_for_server('whois.ripn.net', ['127.0.0.1']); You can specify IPs list which will be used for queries to desired whois server. It can be useful if you have few interfaces, but you need to access whois server from specified ips.


0

After messing around for hours I managed to cook something decent up using zenity and some sudoers changes. I'll give a step by step how I did it all, in case someone else is caught in the ugly situation of being on a linux distro that only has obsolete wireless networking software (like wireless-tools) without any easy way to get a known GUI like wicd... ...


1

I end up with something like this: ssid=$(iwlist wlan0 scanning | awk -F: ' BEGIN{ printf "zenity --list --text \"Available Networks\" --list --column ESSID --column Secure --column Signal "; } /Quality/{ split($0,x,"="); Quality = int(x[2]*100/70+.5); } /Encryption/{ Encryption = $2; } /ESSID/{ ESSID = $2; printf "%s \"%s\" \"%s%%\" ", ESSID, ...


0

Well, the pwhois utility of that module at https://api.metacpan.org/source/NALOBIN/Net-Whois-Raw-2.85/pwhois indicates there is a -a srcip flag, though you're calling whois which may or may not be pwhois.


0

Pipe the output to ts(1) of moreutils among other such similar utilities, which prefix any input with a time stamp: % (echo hi; sleep 3; echo there) | ts Aug 28 18:52:42 hi Aug 28 18:52:45 there % So assuming you want timestamps on just standard error: ...archive.sh 2>&1 >>.../out.log | ts >>.../err.log Note that this is really ...


0

Change it like this: cat input | while read p; do { echo -n "$p "; lynx -dump "www.xyz.com/p?=$p"; } | awk '$2~/Hello/{print $1" "$3}' done Before the first pipe, you can group the commands using command grouping in the form { list; }. The advantage is redirections may be applied to the entire command list. The redirected output looks then like this: ...


1

In bash you can also write that loop with explicit indexing: for ((i=2; i<=$#; ++i)); do process "${!i}" done This iterates over all arguments from the second one to the last one. If you want to exclude the last argument instead, simply make this for ((i=1; i<=$#-1; ++i)); do process "${!i}" done and if you only want to take every other ...


2

If it must be with echo "Kate", use awk: echo "Kate" | awk 'NR==FNR{a=$0;next} sub("Steven", a, $0)1' - file The standard input is Kate from echo "Kate", piped to awk. awk then reads the stdin (-). The condition NR==FNR is true when the first file is processes (stdin). The variable a is set to that value. Then the input of file is processed and sub() ...


2

There's always the caveman approach: first=1 for i do if [ "$first" ] then first= continue fi something with "$i" done This leaves $@ intact (in case you want to use it later), and simply loops over every argument, but doesn't process the first one.


17

First, note that $@ without quotes makes no sense and should not be used. $@ should only be used quoted ("$@") and in list contexts. for i in "$@" qualifies as a list context, but here, to loop over the positional parameters, the canonical, most portable and simpler form is: for i do something with "$i" done Now, to loop over the elements starting from ...


6

I think you want the shift builtin. It renames $2 to $1, $3 to $2, etc. Like this: shift for i in "$@"; do echo $i done


0

This is how you would create a dynamically named variable (bash version < 4.3). # Dynamically named array my_variable_name="dyn_arr_names" eval $my_variable_name=\(\) # Adding by index to the array eg. dyn_arr_names[0]="bob" eval $my_variable_name[0]="bob" # Adding by pushing onto the array eg. dyn_arr_names+=(robert) eval $my_variable_name+=\(robert\) ...


12

It's possible to use signals to communicate between the foreground and background shells: #!/bin/bash # global variable for foreground shell boolean=false # register a signal handler for SIGUSR1 trap handler USR1 # the handler sets the global variable handler() { boolean=true; } echo "before: $boolean" # Here, "$$" is the pid of the foreground shell { ...


8

The & character makes a background process. A background process is executed asynchronously in a subshell. Variables can be passed from a parent shell to sub shell, not the other way around. However, you can do a workaround if you are really in need of the value set in the child shell: boolean=$(mktemp) && echo "false" >$boolean { sleep 5 ...


4

You mistakenly assume that the boolean you set to true in the second line is the same boolean that you test for in the until statement. That is not the case, you start new process, with a new shell in the background and boolean (the one you test for), never gets assigned.


0

For sed lovers — read file1 for each line in file2 it is not good idea. Much better to use variable: my_var=$(<file1) ; sed "s/$/ $my_var/" file2 or more secure read -r my_var <file1 ; sed "s/$/ $my_var/" file2


4

Another awk: $ awk 'BEGIN{getline l <"file1"};{print $0, l}' file2 1 2 3 12 4 5 6 12 7 8 9 12 BEGIN block was executed first before reading input file. The first line in file1 was retrieve using getline() function, stored in variable l With each line of file2, we print it content $0 along with l, separated by OFS, which is a space by default.


0

$ sed -e "s/ *\$/ $(cat file1)/" file2 1 2 3 12 4 5 6 12 7 8 9 12


2

Using awk $ awk -v n=$(cat file1) '{print $0,n}' file2 1 2 3 12 4 5 6 12 7 8 9 12 On csh/tcsh, try: awk -v n=`cat file1` '{print $0,n}' file2 How it works -v n=$(cat file1) This assigns the contents of file1 to the awk variable n. print $0,n This prints each line followed by n. Using sed $ sed '1{h;d}; G;s/\n/ /' file1 file2 1 2 3 12 4 5 6 12 7 ...


0

This should work (in this case file2 gets updated, if you don't want that, remove the -i flag) sed -i "s/.*/& $(cat file1)/" file2


1

The issue was resolved by putting the path before the CLT iwgetwfobj. Thanks for the help.


2

By default, perl read one line input at a time, so your regex never matches. For working with multiple lines input, you have two options. Enable paragraph mode: perl -i.bak -00pe ... or slurp the whole file: perl -i.bak -0777pe ... (Any value above -0400 will cause perl to slurp the whole file, -0777 is used for convention)


1

You need to run dos2unix on your file, which is presumably copied into your Linux distribution with Windows file endings. The ^M, aka the carriage return character, trips your Linux distribution into thinking it needs to look for a command bash^M, instead of bash. To actually view the ^M characters, you can open your file with cat -A: $ cat -A file # --- ...


0

Here's what I learned and how I resolved this seemingly common problem of GEM_HOME and GEM_PATH environment variables not set error. Background RVM install is supposed to create GEM_HOME and GEM_PATH env vars for each RVM that is setup and it does that. But there's a bug apparently such that in some cases the env vars are only set temporarily, and to set ...


0

Count the lines: netstat -nat | egrep 'ESTABLISHED|TIME_WAIT|CLOSE_WAIT' | wc -l Print only those lines: netstat -nat | egrep 'ESTABLISHED|TIME_WAIT|CLOSE_WAIT'


3

Your script can be slightly modified to only process the states you need: netstat -ant | awk '/ESTABLISHED|LISTEN|CLOSE_WAIT/ {print $6}' | \ sort | uniq -c | sort -n A further step would be to everything with awk, e.g. : netstat -ant | awk ' /ESTABLISHED|LISTEN|CLOSE_WAIT/ {count[$6]++} END { for(s in count) { printf("%12s : %6d\n", s, ...


4

How about that: grep -oP 'GET \K.*(?=HTTP)' file | sort | uniq -c | sort -rnk1 | head The grep regex catches everything between the GET and HTTP strings. sort then sorts the output uniq counts them sort again descending, numberical and only the first field. head only prints the 10 first lines (the 10 most visited URLs)


3

How about using established tools like "webalizer" or "awstats" ? You yould process the output of one of these two tools.


4

You could do something like: while IFS=' (' read <&3 ip id name rest; do ssh -n -o ConnectTimeout=3 -o BatchMode=yes -o HostKeyAlias="$name" "$ip" poweroff && while ping -qw 10 -c3 "$ip"; do sleep 1 done done 3< list.txt A few notes on that code: We use the name part as the HostKeyAlias, maybe $id would make more sense. ...


2

ssh host sudo shutdown -h -y now ; ping -c 1 host ; while [ $? = 0 ] ; do ping -c 1 host ; done


3

first create some space by removing files for your (2) question for removing the files from arhive directory find /usr/local/jboss-5.10..GA/server/archive/log -type f -name server.log.* -mtime +60 -exec rm {} \; for your (1) question find /usr/local/jboss-5.10..GA/server/pymidol/log -type f -name server.log.* -mtime +60 -exec mv {} ...


0

It is possible you use the bash as a program, with the parameter -c for execute the commands: Example: bashCommand = "sudo apt update" output = subprocess.check_output(['bash','-c', bashCommand])


0

If you like to achieve this, you need to compare the "ctime" of the files with the "atime" of the script. This cannot be done by a vanilla find(1) implementation but only by an extended find. With a modern find(1) implementation like the BSD find or sfind, you may use -newerXY, in your case: find . -newerca script and this will find all files with a ...


0

A "find" version of Peter's answer to not get subdirs but to actually do all the conversions looks like this: convert_if_no_mp4 () { wav_file=${1} mp4_file="${wav_file%.wav}.mp4" if [[ ! -e ${mp4_file} ]]; then ffmpeg -i "$wav_file" -c:a libfdk_aac -b:a 96k "$mp4_file" fi } export -f convert_if_no_mp4 dir=~/MUSIC find ${dir} -type ...


0

Check following code for your 1st part: #!/bin/bash extFROM=”.wav” extTO=”.mp4” dir="˜/music" tree -d cnt=1 array echo " part 1" find $dir -type d | while read line do array[ $cnt ]="$line" echo ""${cnt}" : ${array[ $cnt ]}" let cnt++; done For second part, can you please give some more information like what you want ...


2

See http://mywiki.wooledge.org/BashFAQ/001 and http://mywiki.wooledge.org/DontReadLinesWithFor for how to read lines that potentially contain spaces and other special characters. There is a ton wrong with how you're handling text in your script. It's subject to word-splitting in a lot of cases that you don't want. See the Wooledge FAQ for more details on ...


0

for putting the whole output of a command into a BASH array, simply do this: array=( $( command ) ) Spare the whole while read part. Be careful with special characters in names though.


0

I tweaked the above ideas some more. It records forever, records all noise until (roughly) a second of silence is encountered. Then: Ensures two subdirectories are created Calculates two spectrographs of the recording - one before normalisation, and one after, and writes to datestamped folders. Writes the normalised one to a file to a datestamped folder. ...


3

I would use zenity with command substitution mechanism: $ shell=$(zenity --list --text "What's your favorite shell?" --title "My title" --column="shells" "bash" "csh" "ksh" "zsh") $ echo "$shell" zsh


2

There are a number of tools for adding GUI interaction to your shell scripts, including: dialog xdialog zenity And there are undoubtedly others.


0

Knowing the checksum of a part doesn't help calculate the checksum of the whole, so you'll have to calculate the checksum of all possible permutations until you find the right one. If you have n parts, there are n! (factorial of n) permutations, and if they're all equally likely you'll have to process n!/2 on average until you find the right one. When you ...


1

perl -MAlgorithm::Combinatorics=permutations \ -le '$i=permutations(\@ARGV); while ($p=$i->next) { $n++; print "combo$n @$p" }' frag1 frag2 frag3 frag4 \ | while read out a b c d; do cat $a $b $c $d > $out; md5 $out; done Or md5sum instead of md5 if you have the GNU tools.


0

How many fragments are there? It sounds like you would have to try every permutation of the fragments (or until you hit the right one), therefore even solving the problem for a relatively small amount of fragments will result in a large amount of work.


16

read_password() { REPLY="$( # always read from the tty even when redirected: exec < /dev/tty || exit # || exit only needed for bash # save current tty settings: tty_settings=$(stty -g) || exit # schedule restore of the settings on exit of that subshell # or on receiving SIGINT or SIGTERM: trap 'stty "$tty_settings"' EXIT ...


16

read -s is not in POSIX. If you want to be POSIX-compliant use the stty -echo. stty and its echo parameter are defined in POSIX. #!/bin/bash stty -echo printf "Password: " read PASSWORD stty echo printf "\n" This will work on all shells that conform to POSIX. Source


0

I am also very new to Shell, and perhaps not fully understood your question. However I managed to get some prompt results into a file using script. Typed script and press enter, then script start recording your shell and writing to a file. Type the command you want the result, example history and press enter. Last you stop your script recording using ...


0

If you need to grep on a arbitrary set of file names that cannot be retrieved by a regular expression: grep -E 'fatal|error|critical|failure|warning|' `cat<<FIN > file1 > file2 > ... > filen > FIN` What's the advantage over pasting the filenames one after another? You can compiled the file name list on a text file and then paste it.


2

I hope i understand it right. cat file1 file2 | grep -E '(^Restoration\ was\ successfully|^Server\ restoration\ is\ complete)' When the files start with single-quotes : cat file1 file2 | grep -E '(^\'Restoration\ was\ successfully|^\'Server\ restoration\ is\ complete)'


0

lspci | awk -F'[][]' '/VGA/{print $2}' Where the field separator is set as [ and ] and the pattern is VGA, print the second field (ie., the match between [..].


1

You probably want to do something like: bail() { echo "$*" exit 1 } command_that_could_fail || bail "command failed"



Top 50 recent answers are included