New answers tagged

0

This is a good case for the pam_env.so PAM module. Virtual console logins use the login program, so you can open /etc/pam.d/login and add a line such as the following: session required pam_env.so readenv=1 envfile=/etc/vc-environment user_readenv=0 With the accompanying new file, /etc/vc-environment: TMOUT=600 Two advantages to using PAM for this: ...


0

Note that {1..20} is not a wildcard/globbing operator. That's a special form of brace-expansion introduced by zsh and copied with limitations and variations by ksh93 and bash. foo_{0001..0030}.nc doesn't expand to the list of matching files, it expands to foo_0001.nc, foo_0002.nc, ... foo_0030.nc regardless of whether the files exist or not. bash has that ...


1

If the top and bottom are fixed, it can be something like: cat top.txt /dev/stdin bottom.txt > text.txt # with cat, - works the same as /dev/stdin or { echo 123 #top.txt cat echo 456 #bottom.txt } > text.txt followed by your compilation commands cat top.txt /dev/stdin bottom.txt > text.txt gcc whatever The first line should be a ...


1

Brace expansion happens before variable expansion, so there's no way to use variables in it. You can use seq instead: seq -f foo_%03.0f.nc $ns $ne


0

Thanks for all the suggestions, I took pieces from everyone's suggestions and built this command. In my case this is exactly the command I required. for f in /mydirectory/*.zip; do n=$(echo ${f##*/} | cut -c1-30); unzip -d /mydirectory/$n $f; done It loops through all the zip files Echos each zip file (file name only, no extra info like size or ...


1

Here is fragment from my working script. send_user "FILE \"FILETEMPL\" is copied to active.\n" spawn scp -l 60 FILETEMPL root@IP:PATHTEMPL/FILETEMPL # After scp we should wait "#" or "100%". set timeout 600 expect { "password: " { send "PASS\r" expect { "100% " {...} "password: " { ...


1

After some searching I found some statement: When bash is interactive, in the absence of any traps, it ignores SIGTERM (so that kill 0 does not kill an interactive shell), and SIGINT is caught and handled (so that the wait builtin is interruptible). In all cases, bash ignores SIGQUIT. If job control is in effect, bash ignores SIGTTIN, ...


0

According to bash's documentation, name A word consisting solely of letters, numbers, and underscores, and beginning with a letter or underscore. Names are used as shell variable and function names. Also referred to as an identifier. word A sequence of characters treated as a unit by the shell. Words may not include unquoted ...


-1

The shell catches many signals and this applies to SIGTERM as well, which is the default signal send by kill. If you really like to terminate the shell via kill, use kill -KILL $$.


-1

The Default signal for kill command is TERM (-15) which signal process to exit cleanly. Try doing kill -9 $$ This will hold up to your expectation and bash/Current Shell will exit.


0

You could use the pathname of your external command "func", e.g. /usr/bin/func.


1

The official way to prevent function definitions from being used by the shell is to call: command func See: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/command.html


0

This would better done using the luhn algorithm a discussion of how to implement it in bash can be found here http://codereview.stackexchange.com/questions/95211/validating-a-credit-card-number-using-luhns-algorithm


1

There are variances and limitations in how regexs get evaluated based on the utility being provided the expression, the arguments used when calling the command, the shell the command is called in, among other issues. With that said, the last section of the regex is what is blocking the match when I use it with grep; it is not listed as optional so it is ...


0

try out this- count=$( $path/to/mysql -h $ip -u $user -p$password $schema -s \ -e "select count(1) from employee where email=xyz@abc.com"); if [[ "${count}" = '0' ]]; then break else echo "Email exist, please enter a new value : " fi


-1

You can get the number of disks and store it in a variable like so: disks=`ls -la /dev/sd* | wc | awk '{print $1}'` You can then iterate through that number in parted: disks=$(expr $disks - 1) ## zero index variable from above for i in $(seq 0 $disks); do parted "/dev/sd$i" print ##### your command of choice done Keep in mind different ...


1

You shouldn't define aliases in /etc/zprofile or ~/.zprofile. These files is only loaded in login shells (“shells” meaning zsh of course). The proper place to define aliases is in /etc/zshrc or ~/.zshrc, which is read by all interactive shells. To run zsh without loading /etc/zprofile, just run zsh with no option. The option -f tells zsh not to read ...


0

In bash You can abuse the --rcfile option like so: bash --noprofile --rcfile <(alias) But the --noprofile is pretty useless without the -l option to make it a login shell.


0

If you don't care about editing the original system scripts, inside the script you can create a function like timeoutf(){ #If hangs 1 minute, dies.. sleep 1m kill $PPID } timeoutf&


2

You can use timeout command to run your command or script in a given timeout. Something similar to this: timeout 10m command Which waits for the command to finish withing 10 minutes otherwise kills it and exits with status 124. Then you can check exit status of timeout and print the appropriate message based on it. See here for more: timeout manpage. If ...


0

I would leave the history file alone and implement your recording of commands in the prompt_command handler instead. I would also make it write to a private log file then you don't have the conflicts with other usages of the history file or issues with other users accessing /root


0

I don't think there is any nice way to do it with just one replacement. But if four replacements are fine, proceed as follows: Replace \A' by  (that is, replace ' at the beginning of the string). Replace '\Z by  (that is, replace ' at the end of the string). Globally replace ([^[:alnum:]])' by \1 (that is, replace every sequence of a ...


1

The reason why your script isn't working is because you are using -le. This causes your script to think that when it reaches 5, it will still execute because it is equal to 5. Change the -le to -lt.


1

Your case statement could look like this S*) echo Starts with S if [[ -f x && -f x.csv ]] then echo File x and x.csv exist else echo input file missing fi ;;


2

If you enter the string literally into the command, you can escape the characters at your will. grep 'Title of Webpage which may include '\'' and "' /var/mobile/Library/Safari/History.plist grep "Title of Webpage which may include ' and \"" /var/mobile/Library/Safari/History.plist Also notice that cat isn't needed. Moreover, it's much better to use an ...


2

As it is speed what you are looking for: We can do one host call with -t ANY instead of the four used now hoping to get all the four resolutions in one. That will need parsing of the answer. If the whois call could be started and while waiting for an answer from the whois servers we can make the host calls to the DNS servers, we can get the fastest ...


2

I’m not an Emacs user but for the last month I’ve being experimenting with Emacs mode in Readline as an alternative to Vi mode. Every time you kill (delete) text, it’s removed from the buffer and added to the kill ring. When you yank (paste) the text with C-Y, the most recently killed text is printed to the cursor position. If you immediately follow this ...


3

M-y undoes the last yank, rotates the lists of all kept kill strings, and yank the new top of the list. Try this example: type the 5 characters a b c (no newline) position the cursor (with C-b) in front of the c and type C-k to kill the c onto the top of the kill ring. Position the cursor 2 chars to the left and type C-k to kill the b, and again to the ...


1

I'm not trying to outdo the accepted answer - there is a good answer already, but this was too long for a comment and I thought it might be useful. A significant speedup would result from reducing the 4 calls to host into one call to dig (or host, but the output is easier to handle from dig). Backgrounding the 4 calls will speed things up as the 4 calls ...


3

That's 4 uses of host and one of whois. The only way you could speed that up would be to run the commands as background jobs and arrange to check for their completion. That would be a rewrite, which the question declines. To make a background job of each, you'd do something like this, redirecting output to temporary files: ( host -t mx $inp ...


-1

if you want to use an if clause, evaluate the count: if (( `ls *.txt 2> /dev/null|wc -l` ));then...


0

Your output contains any amount of random cruft, and one line you are interested in. Select that line, discard anything but the ID and print the result: sed -n '/profile_images/s/.*profile_images\\\/\([0-9]\+\).*/\1/p' This could be made slightly more efficient by quitting immediately after that line has been processed. In fact, that is pretty much ...


1

logs / audits. Logs are the best. Please specify your OS. On Linux, you can use the "audit" system to log all logins. E.g. this is enabled by default on Fedora. Entries appear in the system journal. There is also a dedicated search tool ausearch, which uses a log file. I assume the log is archived periodically (i.e. logrotate); the tool apparently ...


1

You cannot catch errors with trap ... ERR in commands that have their exit code tested. In your particular case, from the bash man page: The ERR trap is not executed if the failed command is ... part of a command executed in a && or || list except the command following the final && ... Generally speaking, the simplest way to ...


4

The fact that you can do something in bash doesn't mean that you should. sh (and bash etc) scripts are best suited to be relatively simple wrappers to launch programs or around text-processing commands. For more complicated tasks, including parsing ini files and acting on them, other languages are more appropriate. Have you considered writing your script ...


0

The right side of a "Variable Assignment" is considered quoted (no splitting or globing): LESS=+'/A variable may be assigned' man bash A variable may be assigned to by a statement of the form: name=[value] Word splitting is not performed, ... . Pathname expansion is not performed. Therefore: $ var=* $ echo "$var" * To get an asterisk to ...


0

It looks like you're using a single-brace implementation of test, but missing the whitespace that is required. You probably have something like echo "Is it morning? Please answer yes or no" read var if ["$var" == "yes"]; then something else something else fi The problem is that spaces are required inside of the braces, thus: if [ "$var" == "yes" ...


2

The shell expands * only if un-quoted, any quoting stops expansion by the shell. Also, a brace expansion needs to be unquoted to be expanded by the shell. This work (lets use echo to see what the shell does): $ echo *.{ext1,ext2} a.ext1 b.ext1 a.ext2 b.ext2 Even if there are files with some other names: $ touch {a,b}.{ext1,ext2} {c,d}.{ext3,ext4} none ...


0

I know it's an incomplete answer but the MySQL.lns in augeas seems to be able to parse most of that. In augtool: augtool> set /augeas/load/testini/incl "/root/test.ini" augtool> set /augeas/load/testini/lens "MySQL.lns" augtool> load augtool> ls /files/root/ .ssh/ test.ini/ augtool> ls /files/root/test.ini target/ = Section1 augtool> ...


1

Another option is to be more specific about what you are grepping for. For example: whois stackoverflow.com | grep -E '^[[:space:]]*(Registr(ar|ant|y)|Sponsoring).*: ' This extracts only lines that begin with optional white space before either 'Registrar', 'Registrant', 'Registry', or 'Sponsoring', followed by any number (zero or more) of any character, ...


0

simply set your parametrs before sourcing the script ! main.sh #!/bin/bash NAME=${*:-"a string"} if [[ -f install.sh ]]; then set -- $NAME ; . install.sh ; fi exit; install.sh #!/bin/bash echo " i am sourced by [ ${0##*/} ]"; echo " with [ $@ ] as parametr(s) "; exit; test u@h$ ./main.sh some args i am sourced by [ main.sh ] with [ some ...


0

I just came across the original answer here by @Mehmet while searching for something unrelated and I see that although it works, it is horribly inefficient, requiring each file to be read again for each unique word in all of the files! The second answer by @Jeff is rather convoluted despite the explanation and worst of all it suffers from the cat file | ...


2

It's a kind of necroposting but I've had the same problem recently (with a different backend) and found that the reason is in a wrong Content-Type. By default it's "text/plain" or "text/html", and in my case curl -H "Content-Type: application/json" -d ... solved the issue.


0

Simplest solution is to cd into the directory where you want to save the files and then run the expect script, leaving out the lcd command from the script on line 8.


-2

ls -ltr | cut -c54- change the column (54) so you only get the data you want.


3

It defaulted to off when it was introduced, in bash 2.02: @@ -119,6 +123,10 @@ int interactive = 0; /* Non-zero means that the shell was started as an interactive shell. */ int interactive_shell = 0; +/* Non-zero means to send a SIGHUP to all jobs when an interactive login + shell exits. */ +int hup_on_exit = 0; + ...


2

Use the -v flag: reg=`whois stackoverflow.com | egrep -i 'Registrar|Sponsoring Registrar|Registrant' | grep -v internic`


1

readlink -f "$(type -P sleep)" or if you're performance-conscious: cpath="$(type -P sleep)"; [ ! -L "$cpath" ] || cpath="$(readlink -f "$cpath")" Using readlink -e (existing) instead of readlink -f can save you from this kind of accident where you operate on a nonexisting file. The second example assumes the path returned by type -P is canonical, which ...


2

Any element of an array may be referenced using following syntax: ${ArrayName[subscript]} You can easily find out bash shell array length using following syntax: ${#ArrayName[@]} Change your code at the bottom to the following: echo "${#live[@]} Processes are running." echo "List of Processes live ${live[@]}" echo "${#dead[@]} Processes are dead." ...


3

$ declare -p > my_environment Later, inside barscript: . my_environment declare -p outputs environment variables in a form that can be executed by the shell, including quoting and escaping of variables as required. $ foobar='"some double-quoted text"' $ echo $foobar "some double-quoted text" $ declare -p foobar declare -- foobar="\"some ...



Top 50 recent answers are included