Tag Info

New answers tagged

1

steeldriver is correct. What you should do is either: Use a command that reads in the standard input: #!/bin/bash fun=$(cat) echo "$fun" Or simply: #!/bin/bash cat Or, to convert standard input into positional parameters, use xargs: $ echo 1 | xargs ./test.sh Or, use the script the way it is supposed to be used (as coded): ./test.sh 1


0

Do this instead #!/bin/bash echo "$@" Then run it like this ./test.sh 1 It will echo 1, don't make it too complicated. And besides, why not just use echo? There is no reason to use this script.


1

zsh's jobs builtin can change the shell's process name. jobs -Z newname


1

You could make your script recursive this way: #! /bin/sh - do-something-with "$1" shift [ "$#" -eq 0 ] || exec "$0" "$@" Then when running your-script a b c, the ps output would show in turn: your-script a b c your-script a b your-script a


0

Try this way: #!/bin/bash sudo sh /opt/scripts/runp.sh & sudo sh /opt/scripts/runt.sh & sudo sh /opt/scripts/rund.sh & ssh sut@slave sudo sh /opt/scripts/runs.sh & ssh sut@slave /home/sut/pf/server-sysfs 8989 & sudo sh /opt/scripts/runc.sh &


0

Below python script does the job, days from on which the files should be deleted can be configured with days variable. #!/usr/bin/env python3 import os import re import datetime days=60 delta = datetime.date.today() - datetime.timedelta(days=days) files = [ x for x in os.listdir() if re.search('_\d{8}\.', x)] for file in files: date = ...


0

Below awk program will help you. #!/usr/bin/awk -f FILENAME == "file2.txt" { if (FNR > 1) { city[$1]=$2 } } FILENAME == "file1.txt" { if (FNR > 1 ) { print($1, $2, city[$3]) } else { print } } When executing the script you have to give the file2.txt before file1.txt as the mapping has to be built ...


1

OK, I have remade this script, and by sorting it backwards it looks like it should work. It compares the year and month to the previous one, and if it is lower it should be the last entry for that month. #!/bin/bash #the tac reverses the listing, so we go from newest to oldest, vital for our logic below FILES=`ls | tac` #create a cutoff date by taking ...


0

You can try this in while loop: #!/bin/bash A=vtm_data_12month_20140301.txt B=`ls vtm_data_12month_20140301.txt | awk -F "_" '{print $4}' | awk -F "." '{print $1}'` C=`date --date="60 days ago" +%Y%m%d` if [ "$B" < "$C" ] then rm -fr $A else echo "$A is not older" fi


1

using awk: awk 'NR==FNR{a[$1]=$2}NR>FNR{if($3 in a){print $1,$2,a[$3]}}' file2 file1 age name pincode 23 ABC 001 25 xyz 002 12 xxx 003 21 YYY 001


1

Use the shell's suffix removal feature str=/opt/oracle/app/oracle/product/12.1.0/bin/tnslsnr path=${str%/*} echo "$path" In general, ${parameter%word} removes word from the end of parameter. In our case, we want to remove the final slash and all characters which follow: /*. The above produces: /opt/oracle/app/oracle/product/12.1.0/bin Use dirname ...


0

start cmd:> dirname "/opt/oracle/app/oracle/product/12.1.0/bin/tnslsnr" /opt/oracle/app/oracle/product/12.1.0/bin file_path="/opt/oracle/app/oracle/product/12.1.0/bin/tnslsnr" dir_path_woslash="${file_path%/*}" echo "$dir_path_woslash" /opt/oracle/app/oracle/product/12.1.0/bin shopt -s extglob dir_path_wslash="${file_path%%+([^/])}" echo ...


0

The following code does not preserve the last file of each month. #! /bin/bash cmp_timestamp=$(date --date="60 days ago" +%Y%m%d) while read filename; do [[ $filename =~ _(20[0-9][0-9][01][0-9][0123][0-9])\. ]] timestamp=${BASH_REMATCH[1]} printf "%-40s : %s\n" "$filename" "${timestamp}" if [ "$timestamp" -lt ...


4

You can use the combination find, grep and awk command to get the desired result. The below is a oneliner which will print the file which has the maximum temperature recorded. find . -mindepth 3 -exec echo -n "{} " \; -exec grep "PROCESSOR_ZONE" {} \; | awk '{ split($4,val,"/"); gsub("C","",val[1]); if (max<val[1]) {file=$1; max=val[1]} } END ...


1

Simply you can sort it by using sort command sort -r version.log | head -n1 | awk '{print $2}' Output: /opt/oracle/app/oracle/product/12.1.0


4

You can do it with AWK. $ awk '{if(max<$1){max=$1;line=$2}}END{print line}' file /opt/oracle/app/oracle/product/12.1.0 Here first column of each line is compared with the variable max (which is initially 0). If the first column has a value greated that max then the second column is stored in the variable line, this continues for each and every line of ...


2

sort -k1 -n filename | tail -1 | awk '{print $2}'


2

awk '$1 > max { max = $1; output = $2 } END { print output }' version.log


-2

This should work: awk -v max=0 '{if($1>max){want=$2; max=$1}}END{print want} ' version.log The -v max=0 sets the variable max to 0, then, for each line, the first field is compared to the current value of max. If it is greater, max is set to the value of the 1st field and want is set to the current line. When the program has processed the entire file, ...


0

On closer inspection, you could use either the Cartesian product or simple permutations. With Python, you could do: import itertools for i in range (min_depth, max_depth + 1): passwords = itertools.product (phrases, repeat=i) for pw in passwords: print (''.join(pw)) (using itertools.permutations instead of itertools.product as per ...


-1

Thank you SteelDriver and Networker! if you don't know the length of the string, try: $ x="lkj" $ echo "${x%?}" lk


0

There is no criteria about output classification, e.g. this tree output is from a previous download with wget results ├── 1 │   └── 2014 │   ├── 2 │   │   ├── 0 │   │   │   ├── st0 │   │   │   └── stout │   │   └── 1 │   │   ├── st1 │   │   └── strout


0

I tend to do something like this: if ! which dos2unix > /dev/null; then echo "Error: dos2unix not installed!" exit 1 fi


0

That will depend on how the content is arranged in that file, however you'll just have to keep track of whether you've opened that file in the script. # Example where the first time opening, line 1 is printed, and subsequent times # line 2 is printed file_opened=0 # ... code if [ $file_opened -eq 0 ]; then # code for first time opening, e.g.: head ...


1

You can use the $RANDOM variable from within Bash scripts to get something different each time you call it. It returns integers (0-32767) but you could test to see if the numbers are even or odd and then make that into a yes or no. $ echo $RANDOM 2104 $ echo $RANDOM 25188 Even/Odd Example $ if (( RANDOM % 2 )); then echo even; else echo odd; fi even $ ...


2

The error is in these lines (which occur twice): if [ $1 -eq 1 ] ; then echo ' The single quote must be on the same line as echo. If it is in the next line then is it not treated as argument to echo but as the next command.


3

It's not possible in general, because a script can contain something like read $command "$command" -rf / In real life, the command would be sanitized or picked from a list, but still, it's not possible to know in advance what commands are possible.


0

Try doing this explicitly : test_apps="command1 command2 command3" for cmd in $test_apps; do type &>/dev/null $cmd && echo "$cmd installed" || echo >&2 "$cmd not installed" done Or if you put your commands with full PATHs : grep -oP '^\s*/.*/\K.*' script.sh | xargs -I% which % 2>&1 | grep -oP ...


1

It should also report -bash: [: missing `]' or -bash: [0: command not found Insert whitespace where needed. In bash, [[ ... ]] conditions are generally easier to use than [ ... ].


2

You can compile this c++ code for quite quick results. It completes in around 0.19 - 0.27 seconds on a 1000 line file. It currently reads 10000 lines into memory(to speed up printing to screen) which if you had 1000 characters per line would use less than 10mb memory which i wouldn't think would be a problem. You can remove that section completely though ...


1

With dd you can reliably read a single byte from a file. With stty you can set a min number of bytes to qualify a terminal read and a time out in tenths of a second. Combine those two and you can do without sleep entirely, I think, and just let the terminal's read timeout do the work for you: s=$(stty -g </dev/tty) (while stty raw -echo isig time 20 min ...


3

If you want to just pause the script whilst remaining inside the script then you can use read instead of sleep. You can use read -t to set a timeout for the read read -n to read one character(effectively just press any key) to continue script As you haven't provided any code, below is an example of how it could be used. If q is pressed then read -n1 ...


5

You don't need to add something to your script. The shell allows such a functionality. Start your script in a terminal. While is is running and blocking the terminal use ctrl-z. The terminal is released again and your see a message that the process is stopped. (It is now in the porcess state T, stopped) Now do whatever you want. You can also start other ...


1

find . -type f | xargs sed -i 's/abc/xyz/g' Use -maxdepth option if you don't want the action to take place recursively in your current working directory.


0

$? gives you the status of last executed command:called as exit status if success its 0 else can be any number root@hackaholic:~# echo "hello" hello root@hackaholic:~# echo $? 0 root@hackaholic:~# ps PID TTY TIME CMD 21005 pts/2 00:00:00 bash 21051 pts/2 00:00:00 ps root@hackaholic:~# echo $? 0 root@hackaholic:~# junk bash: junk: command not ...


1

Another alternative (piping two tr commands): ls -l partition | cut -c5-7 | tr -dc rwx | tr rwx cse


2

The problem is: When bash is started non-interactively then it does not read ~/.bashrc. Thus you have to read h() from the script. If you want to use aliases in a script then you have to enable the use of aliases in the script with shopt -s expand_aliases define the alias in the script. An alternative to an alias is defining a shell function (which ...


0

You seem to have several problems. The free-standing backslashes (\) should be removed. You may use backslashes at the ends of lines to continue commands onto continuation lines; e.g., command1 \ command1 args \ command1 continued # No backslash on last line command2 \ command2 args \ ...


3

All the processing done by SLURM (by sbatch, specifically) is done before bash is invoked, so bash won't help you here. The script could be in any language, it wouldn't matter: the #SBATCH are only coincidentally bash comments, what matters is that they're sbatch directives. Options can be specified in the file so as to provide a convenient way to always ...


-1

You can use get the pid running location using the below command ls -l /proc/PID_id/cwd


2

Alternative way without ls: getfacl -c partition | sed -n '/group::/{s/.*:://;y/rwx/cse/;s/-//g;p;}'


3

I think you want this command: ls -l partition | cut -c5-7 | tr rwx cse |sed 's/-//' You can remove the one extra command(cut -d ' ' -f 1) and replace it with your last cut command(cut -c5-7) and also add sed 's/-//' at the end to remove all -s. Now you are done. you didn't need to adding extra |. And even better: you can also change the dash(- ...


2

You can ask bash for help in answering these questions too, using set -x. This flag tells bash to display the command and its expanded arguments before running it: First, some setup: $ a=some-value $ b=some-other-value $ set -x Now let's see how the shell interprets these commands: $ a=b + a=b The value of the variable a is being set to the literal ...


-1

I ended up using the following: grep -r "$command" $(ls -l /proc/$pid/cwd | awk '{ print $11 }') | awk -F: '{ print $1 }' Where $command="$(cat /proc/$pid/cmdline | sed 's\x0/ g' | sed 's/.$//')" Which will recursively grep through the files in the directory that the script is in to find the file containing the command line that ran the nc command. Seems ...


0

You have some missing pieces of your paths it looks like. On top of that, it's just plain messy code. Good code should be clean and easy to read. You have way too many '/'s in your example. Clean that up, and it will be easier to see where the differences are. In the first command: nohup java -Xmx3g -jar GenomeAnalysisTK.jar -T RealignerTargetCreator ...


1

To get the parent PID of the process, portably (POSIXly), you can use: ps -p "$PID" -o ppid= or (on Linux): grep '^PPid' "/proc/$PID/status" |cut -f2 for more ways, see http://superuser.com/questions/150117/how-to-get-parent-pid-of-a-given-process-in-gnu-linux-from-command-line


6

$ a=b $ printf $a b $ b=hello $ a=$b $ printf $b hello Basically a=b makes a variable called a of which value is a literal b. a=$b makes a variable called a with the same value as an already existing variable called b.


3

In case of, a=$b you are assigning the value of the variable b to a. While in case of a=b you are assigning a the value of the literal string "b" E.g. b=10, Now suppose you want to assign a the same value as b. You can do either of the following: a=$b or, a=10


1

I would use sed it's really powerfull, this bash file would change the values: #!/bin/bash path_to_conf="/path/to/vsftpd.conf" anonymous_=NEIN local_=JA chroot_=IDK sed -c -i "s/\("anonymous_enable" *= *\).*/\1$anonymous_/" $path_to_conf sed -c -i "s/\("local_enable" *= *\).*/\1$local_/" $path_to_conf sed -c -i "s/\("chroot_local_user" *= ...


1

In bash, ksh should work as well, using only shell built-ins: #!/bin/bash # we require array support d=( $(< sample.txt) ) # quote arguments and # build up brace expansion string d=$(printf -- '%q,' "${d[@]}") d=$(printf -- '%s' "{${d%,}}' '{${d%,}}") eval printf -- '%s\\n' "$d" Note that while this holds the entire file in memory in a shell variable, ...



Top 50 recent answers are included