Tag Info

Hot answers tagged

18

rm !(*.sh) is a extglob syntax which means remove all files except the ones that have the .sh extension. In your interactive bash instance, the shell option extglob is on : $ shopt extglob extglob on Now as your script is running in a subshell, you need to enable extglob by adding this at the start of the script : shopt -s extglob So your ...


10

Some syntax errors The base for the 'if' constructions in bash is this: if [ expression ]; then # code if 'expression' is true. fi here is the corrected version if [ "$(ipcs|grep test|wc -l)" -eq 630 ]; then echo "Shared Memory check passed" fi or with [[…]] to prevents word splitting of variable values without douple quotes if [[ ...


7

Essentially there are multiple ways to construct if conditionals in bash; one is outlined in A.B.'s answer; another one, which is likely the one you are trying to use, involves using double parentheses, which is a way to manipulate expressions / variables in bash in a more of a C-style way, and which, per its definition, requires double parentheses in order ...


5

arp manipulates the entries in the kernel ARP cache, you are probably going to get a lot of entries (depends on the size of the LAN). As you are only interested in the IP addresses you should use the -n option so that arp shows numeric addresses instead of hostnames first. Also you will typically get a lot of IP addresses depending the size of your LAN, so ...


5

Here is another idea using awk arp -ni eth0 | awk 'NR>1 {print $1}' Example $ arp -ni eth0 | awk 'NR>1 {print $1}' 192.168.20.3 192.168.20.1 192.168.20.11 or with a variable $ addresses=$(arp -ni eth0 | awk 'NR>1 {print $1}') $ echo $addresses 192.168.20.3 192.168.20.1 192.168.20.11 To get a specific index without an array $ arp -ni eth0 ...


5

Ok, it's a cross-post, but I have to write an answer. ;) You could use find instead find . -maxdepth 1 ! -name '*.sh' -exec rm -rf {} \;


5

When you do ./script.sh the script is run in a subshell. So any operation/change that the script is meant to do is taken place at the subshell, hence the parent shell i.e. from the shell the script was called from remains unaffected of the changes. On the other hand, when you do . script.sh (. is the same as shell builtin source), the script is source ...


4

Using sort and awk Suppose your date format is the following 08/03/2015 –> Day, month, year using the command below. It's one command (note the \ at the end of the lines) Replace your_input_file with your file name. The command awk '{for (i=1;i<=NF;i++) {if (i==3) {printf "%s/%s/%s\t",substr($3,7,4),substr($3,4,2),substr($3,1,2)} else {printf ...


3

To add another option: you can do away with square brackets and parentheses altogether by using the test built-in instead: if test $(ipcs|grep test|wc -l) -eq 630; then echo "Shared Memory check passed" fi The [ built-in essentially is an alias for the test built-in, except that it takes one additional argument: ]. This also illustrates that the [ ...


3

Both execute the script that is located in the same directory that you are in, i.e. your current working directory. Doing . script is referred to as sourcing the script in bash From http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x237.html When a file is sourced (by typing either source filename or . filename at the command line), the lines of code in ...


3

See man pgrep. since I don't have ArcaDirect, but do have NetworkManager (0)$ pgrep NetworkManager 1400 (0)$ pgrep ArcaDirect (1)$ This is what I typed at the terminal (0)$ pgrep NetworkManager 4011 (0)$ pgrep ArcaDirect (1)$ if $(pgrep NetworkManager >/dev/null) ; then echo "Running" else echo "Restart needed" fi Running (0)$ if $(pgrep ...


3

You can solve it using eval. eval - construct command by concatenating arguments Modify your script as follows: #!/bin/bash source /data/output/QAScripts/conf.ini eval var='$'"$1"path echo $var then run it like this: $ ./foo LST /data/bb/loader where, $ cat /data/output/QAScripts/conf.ini LSTpath=/data/bb/loader


2

Once the question has been changed and it was unclear what is meant, the 2nd part of my answer is an answer to this version of your question. But I will try it again Using awk #!/bin/bash path=$(awk -F= '/path/ {gsub (/\[date\]/,"'"$1"'",$2); print $2}' conf.ini) echo $path Example % cat conf.ini path=/var/opt/[date]/yellow % ./foo 08/20/2016 ...


2

You can use programs from metasploit to generate shellcode. Here's an example using msfpayload to do so: http://www.hacking-tutorial.com/tips-and-trick/generate-a-shellcode-using-msfpayload-metasploit-command-line-instance/


2

Correct the following issues and check your script again here or see my corrections below 1 #!/bin/bash 2 3 source conf.ini 4 5 inotifywait -m -e create /$1path | 6 while read file; do 7 8 if ($(echo $word | head -c 1)"=$1 then ^––SC1009 The mentioned parser error was in this simple command. ...


2

In don't know what you are trying to do with this script but looking at it first time i see the errors are because of this line : if ($(echo $word | head -c 1)"=$1 then Make it as : if [ "$(echo $word | head -c 1)" = "$1" ]; then while doing a check with if-then conditional construct use test ([) or [[ (bash-ism). Check help test for more info. When ...


2

Another awk version, with arrays. A bit more lengthy but working awk -F':' ' $3 >= 1000 && $1 != "nobody" {i++;humanuser[i]=$1 } $3 < 999 { k++;sysuser[k]=$1} END {printf"****HUMAN USERS\n";for (j=1;j<=i;j++) printf humanuser[j]" "; printf "\n*****SYSTEM USERS\n"; for(m=1;m<=k;m++) printf sysuser[m]" "}' /etc/passwd Sample output: ...


2

Change the line in the sudoers file to: kf ALL=(ALL) NOPASSWD: /sbin/fstrim I don't recommend, adding the script in /etc/sudoers, because the script can be altered and every command (the whole script) would then be executed with root privileges.


2

Nice question. The main issue in your question The main issue in your question is that you actually want to sort by two fields (date, time), of which one needs to be reverted (read backwards) before sorting, since the date is in the format dd/mm/yyyy. If we do that (in a verbose way for clarity reasons), the script below does that. It: first reverts the ...


2

Assuming 08/03/2015 means8th March, 2015, you can use this bash one liner : while IFS= read -r line; do parts=( $(echo "$line") ); printf '%s %s\n' "$(date --date="$(sed -r 's_([^/]+/)([^/]+/)_\2\1_' <<<"${parts[2]} ${parts[3]}")" '+%s')" "$line"; done <file.txt | sort -k1,1n | cut -d' ' -f2- Expanded form : while IFS= read -r line; do ...


2

save this as run_gedit.sh #!/bin/bash gedit & then run chmod 755 run_gedit.sh and you should be able to start it by clicking on the file.


2

Expanded version of my comment: You can use sed to edit your file. In general you need: sed 's/STRING/REPLACEMENT/g' In your example the following code should work for you: With explanation: sed -i # the -i option allows you to read from and write to the same file 's/none luks,discard/ # the part you want to replace ...


2

You need to turn extglob on: shopt -s extglob


2

When the script /test/testcmd runs on the remote host it needs a /test/PROCESSLIST file in the remote host, in the same way it needs it in the local host (while [...]; do [...]; done < /test/PROCESSLIST). That's what the error is about, but going further the script is going to break even when fixed this, since it also needs multiple /test/$proc files, ...


1

You should specify the full path to sqlplus. Try to find the path by running: env | grep -i ora | sort Or just try: which sqlplus


1

To rename files, it is probably simpler to use perl's rename utility (sometimes called prename). I will assume, though, that you are doing this as an exercise in learning shell scripting. To fix the immediate problem, all you need is a second shell variable, here called new: for file in * do new=$(echo "$file" | sed -e 's/ */_/g' -e 's/_-_/-/g') ...


1

ssh test@Testhost ./testprocess The ssh client takes an optional command and parameters. You could experiment: ssh test@Testhost ls ssh test@Testhost ls -l This works best if you have set up ssh for password-less login using public keys.


1

DISCLAMER: The following java answer is for educational purposes and the fun of coding only. If you downvote, explain in the comments why. Code The code bellow reads each line from /etc/passwd , splits that line into strings with : as separator, and sorts users depending on their UID into appropriate ArrayLists. package com.askubuntu.users.serg; import ...


1

Install Unity Tweak Tool to customize the behavior of Unity Launcher. Open a terminal and execute : sudo apt-get install unity-tweak-tool Open the tool, click on Launcher Tab under category Unity and tweak.


1

This awk command does what you want. First, you set the record separator to : with FS=":", check if the third element is 1000-60000, and if so, print element 1 (username): awk 'FS=":" {if ($3 > 999 && $3 < 60001) print $1}' < /etc/passwd



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