Tag Info

Hot answers tagged

14

The function returns, but the command substitution blocks, because you created a background job, but you still have your stdout fd opened. Just close it by adding >/dev/null before the &. #!/bin/bash function start { leafpad >/dev/null & echo $! } PID=$(start) echo "PID is $PID" If you want your process to have also stdin, stdout, stderr ...


9

First check if the path to add is already part of the variable: [[ ":$PATH:" != *":/path/to/add:"* ]] && PATH="/path/to/add:${PATH}" If /path/to/add is already in the $PATH, then nothing happens, else it is added at the beginning. If you need it at the end use PATH=${PATH}:/path/to/add instead. Edit: In you case it would look like this: [[ ...


7

The problem is sudo -s without any argument will open an interactive shell for root. If you just want to run a single command using sudo -s, you can simple do: sudo -s command For example : $ sudo -s whoami root Or you can use here strings : $ sudo -s <<<'whoami' root If you have multiple commands you can use here doc : $ sudo -s ...


7

That's because you are checking whether the string gpio -g read 22 is greater than 1. Since gpio -g read 22 is not a number, you get that error. You don't explain what you are trying to do but I'm guessing you want to compare the output of the gpio command. To do that, you need to enclose the command in $() or backticks (``): x=$(gpio -g read 22) if [ ...


6

sort -n -k2 -k1.3 file | awk '{$2!=a?x=1:x++} {print > "file"x; a=$2}' First , we need to sort the file correctly. -n sorts the file numerically, -k2 sorts according to the second field (the marks 2-6), -k1.3 then sorts within this order the first field starting from the 3rd character numerically (irgnoring the leading Q.). Now awk splits the output ...


6

The test [[ -t 1 ]] returns true if File descriptor 1 (STDOUT) is opened on the terminal, otherwise false. From help test in bash : -t FD True if FD is opened on a terminal.


5

The sysadmin in my old lab had a nifty little function for this: pathmunge () { if ! echo "$PATH" | /bin/grep -Eq "(^|:)$1($|:)" ; then if [ "$2" = "after" ] ; then PATH="$PATH:$1" else PATH="$1:$PATH" fi fi } It will both check whether the string given is already in the PATH and ...


5

Using Perl compatible regular expression in grep: grep -Po '..(?==Y)' <file Result: p2 p6


4

The command tee, when passed a non-existing file as parameter, will create that file before writing the output to it. By prepending that command with sudo, you are asking your shell to run the tee command as root. The consequence is that the file created by tee is owned by the user starting the command: root, and is therefore read-only for other users. You ...


4

Set your Warning options as an array. "${warnings[@]}" generates 3 individual words warnings=(-Wall -Wextra -Wpedantic) "${compiler}" "${warnings[@]}" "${standard}" -o "${1}" "${1}.cpp" Or, if you find it more legible, you can create the array without -W's, and then add -W's via how you present the array on the command line. warnings=( all extra ...


4

No need for a script. It's a one-liner with a minimum of awk : df -hlP | awk 'int($5)>60' If you want to change the output, just use awk 'print' : df -hlP | awk 'int($5)>60{print "Partition "$1" has only "$4" free}' And pipe it to your mailer : df -hlP | awk 'int($5)>60{print "Partition "$1" has only "$4" free}' | mail -s "Disk usage on ...


3

In bash, there is an option that instructs bash to record a timestamp with each command. You do this by assigning a useful value to the environment variable HISTTIMEFORMAT. From the bash manpage: If this variable is set and not null, its value is used as a format string for strftime(3) to print the time stamp associated with each history entry ...


3

an awk answer: this will keep the order the questions the same as in the source file. $ awk '{filename = "questions" ++n[$2] ".txt"; print > filename}' questions.txt $ cat questions1.txt Q.1 2 Marks Q.2 5 Marks Q.3 4 Marks Q.4 3 Marks Q.5 6 Marks $ cat questions2.txt Q.6 4 Marks Q.7 3 Marks Q.8 2 Marks Q.9 6 Marks Q.10 5 Marks


3

This is a really weird requirement. Why would you care what interactive shell invoked your script, or invoked some other program that isn't an interactive shell which in turn invoked your script? This has a very strong smell of a XY problem. IF you really need to know, you can try to figure it out, but I don't think there's a fully reliable method, just one ...


3

In your link, the variable used outside (linecount) is not defined as a "loop" variable. It's just modified (incremented) inside the body loop, but not in the "while" statement. This is because when the "read -r f1 f2..." part is called, it reset (prepare) the variables used (f1 ..f7), wait for an input line and assign the variables according to the input. ...


3

[ -t 1 ] ...or... test -t 1 ...return true if file descriptor 1 - stdout - is a tty and false otherwise. The same is true of [[ -t 1 ]] ...in many shells.


3

Try this: echo 'p1=X||p2=Y||p3=X||p4=X||p5=X||p6=Y||p7=X' | grep -o '[^|]*=Y' | cut -d= -f1 | sed -e 'N;s/\n/,/g' Output: p2,p6


3

For ksh93, you have (at least) a couple of choices associative arrays envir=Dev foo["$envir"]=bar echo "${foo["$envir"]}" namerefs nameref var=${envir}foo var=bar echo "$var" For ksh88, you may be stuck with eval: envir=Dev name="${envir}foo" eval "$name=bar" eval "echo \$$name"


2

{001..005} is a bash feature but you are running your script with sh and on Ubuntu sh is dash not bash. So don't run your script with sh script.sh but with bash script.sh or even simply ./script.sh since you have #!/bin/bash at the beginning of your script.


2

Since the number of files is large, awk sounds like a good choice: awk ' {line[FNR] = line[FNR] $1 OFS} END {for (i=1; i<=FNR; i++) print line[i]} ' file1 file2 file3 ...


2

I think you have too many quotes and in the wrong places. The following will try run somefile.php and not set IMPORTCOMMAND as there is a space between php -f and somefile.php. IMPORTCOMMAND='php -f' "$LOCATION"'/somefile.php -- -process'"$INPUTFILE" Should be: import_command="php -f $LOCATION/somefile.php -- -process $INPUTFILE" output=$( ...


2

Just run wc -l on the output for all the files: #!/bin/bash - files=(/share/capture/*.pcap) for f in "${files[@]}"; do tcpdump -qr "$f" 2> /dev/null done | wc -l See also: capinfos -rTc "${files[@]}" capinfos -rTc "${files[@]}" | cut -f2 | paste -sd + | bc (assuming file names don't contain TAB or NL characters). Note that what you do: ...


2

It looks like the script was running php as an application instead of trying to get the version number. You should be able to fix it by changing the line to read: if [[ "`$OPENSHIFT_RUNTIME_DIR/bin/php-cgi --version`" =~ "${DIST_PHP_VER}" ]] ; then The =~ test is a regular expression match, part of bash. From the bash(1) man page: An additional ...


2

Try something like this: #!/bin/bash if pgrep "mysql" > /dev/null then echo "MYSQL Running" rm -f /var/run/.mysql_mail_sent else echo "ALERT Stopped" if [ ! -f /var/run/.mysql_mail_sent ]; then // send your mail here date > /var/run/.mysql_mail_sent fi fi


2

You check its options. [ "$-" = "${-#*i}" ] || echo shell is interactive You can also check its file descriptors. This is a little different. It won't necessarily tell you if the shell is interactive per se, but it will tell you if it's talking to a terminal. for fd in 0 1 2 do [ -t "$fd" ] && break done|| echo shell fds 0 1 2 are ...


2

You could just do the whole thing in awk. The following combines the IDs as they were read. awk '{ line = $0; # ID is { XXX } or ( XXX ) if ( /[{(] *[0-9]+[})]/ ) { id = $0; sub(/ *[})].*/,"", id); sub(/.*[({] */,"", id); } # ID is ID=XXX else if ( $NF ~ /ID=/ ) { id = $NF; ...


2

Your line 17 should be: if [ $usage2 -gt 6 ] You need to put a space after [ and before ]. Also, when comparing numbers, you should be using -eq, -ne, -gt, -lt, -ge, and -le rather than the familiar symbols.


2

You want (without any spaces around the =) usage2=$(awk '{print $5}' /home/peters/usage1 | tail -n1 |cut -c1) to put into the usage2 variable the output of your awk...|cut -c1 command. You need a space between the awk braced command and its input file name (but no spaces around =). BTW, you could simply use cut(1) as cut -f5 (instead of awk '{print ...


2

You have two basic choices: i) use -v to pass the variable to awk or ii) close the ' around the awk script, use the shell variable and continue the ' again. Use -v while read city do awk -vcity="$city" -F, '{ if ( $1 == "ACTIVE" && $2 == city ){print $1} }' siteDBName >> count ...



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