Tag Info

Hot answers tagged

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


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


11

The best kind of approach here is to do something like: mysqldump --defaults-extra-file=/path/to/auth.cnf ... Where auth.cnf looks like: [client] user=the-user password=the-password Then make sure the file is only readable by whomever is meant to run that script. The script itself can be world readable.


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


5

With sort (it will also sort the output): sort -u file With awk (it doesn't sort, just filter out the duplicates, like the desired output): awk '!a[$0]++' file With perl (same principle as the awk one): perl -ne 'print unless $a{$_}++' file


5

dumpcap, the low-level traffic capture program of Wireshark, can be instructed to stop capturing after certain conditions with the option -a. You can stop capturing after writing 60MB. This isn't the same thing as measuring traffic, since it depends on the file encoding, but it should be close enough for most purposes (and anyway the exact traffic depends at ...


5

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


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


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)


4

On Linux, /run or /var/run are the standard places for things that are not required to survive a reboot, but mustn't be removed by e.g. tmpreaper. Daemons generally keep their pid files in there, for instance. If your script runs as an ordinary user, then a dot-directory in that user's home directory is probably the right choice. That's where applications ...


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.


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.


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


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


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


3

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


3

The echo command will repeat any string it is fed as argument while interpreting wildcards and variables: echo ABC $USER result ABC myusername So now you can just print the variable that was read in with read -p "Enter variable" var to the terminal via echo $var or with additional text as echo "ORACLEVAR=$var" result: ORACLEVAR=userinput for ...


3

I think you want the shift builtin. It renames $2 to $1, $3 to $2, etc.


2

You could use mogrify to batch convert & resize all .png images in the current directory: mogrify -resize 25% -format jpg *.png because it uses -format jpg the original .png images are left untouched (when format is the same or missing the originals are overwritten).


2

Leaving a plain text password in any file is always a bad idea in case your system is ever compromised. Sometimes it is unavoidable. To make this "secure" you should limit this activity to a very limited user and also leave these sensitive options in a defaults file. To solve your issue specifically, from the mysql man page: If you use the short option ...


2

# systemctl start postgresql.service Some environments would translate service <name> start to systemctl start <name>.service, but you don't have to rely on it.


2

Your function call makes no use of the arguments at all. Did you mean: function run_wine { WINEPREFIX=/disk1/.wine-ptgui \ WINEDLLOVERRIDES=mscoree=d \ /software/wine/1.7.42/linux.centos6.i386/bin/wine \ /disk1/.wine-ptgui/drive_c/Program\ Files/PTGui/PTGui.exe "$@" } (with the "$@" at the end)?


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)'


2

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


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


2

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


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)


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


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



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