New answers tagged

0

for macOS users: open ~/.bash_profile at the end of the file add the following line and save it export PS1='\u:\w\$ ' result at home directory: user: ~$ here u for user w for current working directory $ is to prompt to display you can try the following styles: export PS1='$ ' to just have $ as prompt, nothing else. just like: $


3

Since you're already processing this in awk, you may as well do the whole thing directly: $ echo "foo bar :29.06.2019 23:03:17" | awk '{sub(/^:/,"",$3); print $3,$4}' 29.06.2019 23:03:17 The sub command's general format is sub(/REGEX/, REPLACEMENT, TARGET) and will replace all matches for the regular expression REGEX with the string REPLACEMENT in the ...


3

To cut off the first character, you can also use cut -c: $ echo ":29.06.2019 23:03:17" | cut -c 2- 29.06.2019 23:03:17


3

Use cut -d: -f2- instead of cut -d: -f2 to get anything from the second field to the end of line: TDS="$(grep 'Logfile started' process.log | awk '{print $3,$4}' | cut -d: -f2-)" echo "$TDS"


4

Here is a sed solution: $ echo ':29.06.2019 23:03:17' | sed 's/^://' 29.06.2019 23:03:17 What the command sed 's/^://' is doing is substitute s the colon character : from the beginning ^ of each line with the empty string //. Here is a tricky awk solution, where we changing the field separator to ^:, described above, and output the second field (of each ...


0

Guessing you have a php script that contains some input handle like this fopen ("php://stdin","r");, if you want a bash script to 'type in' a 'yes', you may simply use pipe redirection: #!/bin/bash # some commands echo "yes" | /usr/bin/php /path/to/php/script.php #


3

You’re missing a leading slash making the shebang an absolute path: #!/bin/bash # ↑ here In your case, the shell seems to be searching for ./bin/bash. The shebang (and also executable permission) is only taken into account if you’re running the script as a program: $ ./test1.sh It is ignored if you directly run the interpreter and provide your script as ...


0

You said you were learning bash so I thought you might appreciate a different kind of answer using zenity which is a popular GUI for bash built into Ubuntu. Zenity Dialog Box Here's what the zenity file selection dialog box looks like: Calling the script Here is how to call the script and what it outputs to your terminal when you select an item on the ...


1

Here's a minimal working example. Note the following: tilde (~) doesn't expand inside double quotes, so needs to be outside. Consider replacing download=~/"Downloads" by download="$HOME/Downloads" don't use ls to generat a list of filename - it will break if any of the filenames has whitespace. Use a shell glob *, and put the results in an array So: #!/...


0

The select command will try to display every field separated by spaces, tabs, and newline characters. So I don't think your sample will do what you want. You must not use "ls -l" for long listing or the permissions and ownerships and file sizes will all be fields displayed in the select statement results. Not sure if this is exactly what you were looking ...


0

In psuedo code: LastLine="" SecondLastLine="" while read LineIn SecondLastLine="$LastLine" LastLine="$LineIn" done # now string compare last lines Typed on phone but I can post non psuedo code answer after work.


2

You could use something like this. It works for files with an arbitrary number of columns, assuming that the first column is text and shall have SUM in the result line instead of the sum of all values of that column. $ awk '{for(i=2;i<=NF;i++)a[i]+=$i;print $0} END{l="SUM";i=2;while(i in a){l=l" "a[i];i++};print l}' data.csv > final.csv A 5 3 B ...


1

Found something that at first didn't work for me on StackOverflow https://stackoverflow.com/a/27110024/2161301 awk -F',' '{ print($0); len=split($0,a); if (maxlen < len) { maxlen=len; } for (i=1;i<=len;i++) { b[i]+=a[i]; } } END { for (i=1;i<=maxlen;i++) { printf("%s,", b[i]); } print "" }...


0

ID=$(aws ec2 describe-addresses --region us-west-2 --query 'Addresses[].AssociationId[]' --output text >> AId.txt) IP=$(aws ec2 describe-addresses --region us-west-2 --query 'Addresses[].PublicIp[]' --output text >> OIP.txt) read -a array <<< $(cat AId.txt) touch NR.txt for i in ${array[@]}; do echo $(aws ec2 describe-addresses --region us-west-2 -...


0

I found the answer here, it turned out I needed to figure out how to install the timezone (tzdata) program non-interactively, rather than cimg. For those interested my script is apt-get update -y apt-get upgrade -y apt-get install -y libx11-dev export DEBIAN_FRONTEND=noninteractive ln -fs /usr/share/zoneinfo/Australia/Brisbane /etc/localtime apt-get ...


0

The same thing was bugging me, so I'll answer this old question. I've found that xdg-open and gio open have trouble with this sort of script, and I've also noticed gnome-terminal has trouble with it. For example, xdg-open can open gedit to edit text files, but not evince to view pdfs. So I use the following script with xterm -e. Note that this will leave ...


2

Actually no need flint. Just do md5sum and a get list of I/O errors, then process to delete those files. ~/usr/mnt5/DCIM/104APPLE$ md5sum * md5sum: IMG_4001.JPG: Input/output error md5sum: IMG_4002.JPG: Input/output error md5sum: IMG_4003.JPG: Input/output error md5sum: IMG_4004.JPG: Input/output error md5sum: IMG_4005.JPG: Input/output error md5sum: ...


0

First remember that given any two lists, there are 3 types of difference that we can calculate: elements that are in List 1, but are not in List 2 elements that are in List 2, but are not in List 1 elements that are in one list, but not both (symmetric difference) The standard Unix tool to compare lists (files) line-by-line is comm. It normally outputs 3 ...


0

I used the combination to get it to do what you want: Generate the arrays with: l1=$(cut -d" " -f 1- src1.txt) l2=$(cut -d" " -f 1- src2.txt) Use the command like this to compare both arrays: l1=$(cut -d" " -f 1- src1.txt) && l2=$(cut -d" " -f 1- src2.txt) && echo "${l1[@]}" "${l2[@]}" | tr ' ' '\n' | sort | uniq -u | xargs -L 2 > ...


0

It seems you have two files with a huge line each, which contains (space) separated tokens. The diff tool is good for line matching. For items within the line you will have to be a bit more creative. For example :~$ cat file1 a b c d :~$ cat file2 b d :~$ cat file1 | sed 's/ /\n/g' |grep -vf <(cat file2|sed 's/ /\n/g') | tr '\n' ' '; echo a c we ...


1

I'm assuming what you want to do is count the number of spaces making up the last field separator (the whitespace between the second-to-last and last columns). Using GNU awk, you can split lines based on content using a suitable regular expression (FPAT), and retrieve the separators afterwards: $ gawk '{n = patsplit($0,a,"[^ ]+( [^ ]+)*",seps); print ...


1

From a regex standpoint, I think you'd be looking for this: \s{2,} -- which means "A space that is two or more". It won't count spaces in between words. It goes from this: To this:


1

Because _ is a valid character for a variable name, you need to use the ${varname} syntax to let the shell know the end of the variable name. Otherwise, you have $i and $_logfile as variables, but the latter is empty. The syntax $i$ does not exist. Use: setsid python main.py 1>data/${i}_logfile.txt 2>&1 Note that $variable is actually a ...


0

There is a nifty utility for precisely this task: https://bitbucket.org/eradman/entr/src/default/


0

You likely shouldn't do that. You can probably achieve your goal in way that does not involve writing one script that generates another script that you will later modify and run; see below. But if you really want to do it, and you want to do it with code that closely resembles what you've shown, then steeldriver is right that a here document is a workable ...


0

Use a here document, making sure to quote the delimiter word so that the shell does not expand anything in the body: #!/bin/bash cat << 'EOF' | sudo tee -a /usr/local/sbin/adduser.local #!/bin/bash lastuser="$(grep home /etc/passwd | cut -d: -f 1 | tail -1)" EOF The use of tee is because sudo echo "..." >> somefile doesn't do what you ...


2

You can use the unzip command with its -l option. man unzip says about the -l option: list archive files (short format). The names, uncompressed file sizes and modification dates and times of the specified files are printed along with totals for all files specified. For example you are in ~/sed directory,in which there is a zip file named my....


0

It looks like you are using redirection incorrectly (output of docker load redirected to alpine.tar): docker load>alpine.tar I would suggest that you download alpine.tar from the other machine again (since it could be corrupt now) and then use docker load -i alpine.tar instead. Since the image isn't loaded correctly, it would explain why the create ...


1

First, you need to enable ForwardX11 on the client and X11Forwarding on the remote host in /etc/ssh/sshd_config. Then, try without adjusting DISPLAY on your workstation/remote machine first. Only set the DISPLAY variable after you've ensured everything else is set up to work correctly. You shouldn't have to set this but it is possible. I believe that the ...


2

For quickly executing commands, use ALT+F2 and type your command there. Be careful as some functionalities like redirecting output into files might not be working as expected.


4

You do this by escaping the newline character, add a backslash \ directly before the line break: python file_a.py file_b.py \ file_c.py file_d.py You may indent the second line to further improve readability: python file_a.py file_b.py \ file_c.py file_d.py


2

The answers so far assume that files only go into a directory and never go out. But what if you have a script that empties the directory periodically? Then the script could miss the fact a file went into the directory 5 minutes ago but was processed and taken out. Another option is using the inotifywait command written in highly efficient C language for the ...


0

ctrl+w does not delete but cuts everything in front of the cursor. Therfore you can yank/paste it with ctrl-y assuming you did not move the cursor. ctrl+_ actually restores the line itself.


1

You are running a bash script as an sh script. You haven't shown your actual script, but based on the error, I am guessing you either have an sh shebang: #!/bin/sh Or you are calling the script with sh instead of bash: sh yourScript.sh The <() operator you are using is a bash feature and isn't available in the sh shell (which is a basic shell called ...


2

Before checking for new files, I would use "stat" on the directory itself and see if the timestamp changes. $ stat $PWD File: ‘/home/paulm/SCRATCH’ Size: 6144 Blocks: 8 IO Block: 32768 directory Device: 26h/38d Inode: 10937103831396301849 Links: 6 Access: (0775/drwxrwxr-x) Uid: ( 520/ paulm) Gid: ( 1020/ paulm) Access: 2019-07-...


1

I fixed some issues in the script and added comments inline. See below: #!/usr/bin/env bash echo "Hello there, i am a little script so you can change a number of txt file names at once" read -p "I would like to know your name before we start, what is your name? " NAME echo "Well hello there $NAME, nice to meet you im Scripty" echo "Let's start" read -p "...


3

Probably the "cleanest" way to solve this is to modify the convert.py script to clean up after itself. An alias is a simple text replacement macro - so your example will expand to something like python /filepath/convert.py && rm *.dng file1.RW2 file2.RW2 file3.RW2 ... which is obviously going to be incorrect. For anything more complex than a ...


1

Some issues here: A function definition in bash needs () after the function's name ${1:-Default Message} returns either the content of $1 (if that is given) or the given default value. Note: it returns it. That is, it is the same as if you had written: if [ -n "$1" ]; then $1 else Default Message fi bash is dumb, so it will try to simply run "...


0

Then you have (at least) two java versions on your machine, right? Besides setting JAVA_HOME you probably want to modify the PATH variable, and you want to change it persistent. Assuming you use e.g. the ~/.profile or ~/.bashrc file for the purpose, you can add these lines: export JAVA_HOME=/usr/java/jdk.1.8.0_60 PATH="${JAVA_HOME}/bin:$PATH" Then relogin ...


0

I faced this same problem recently. The easiest solution I can suggest you is to add this line in the settings.json file of Visual Studio Code. The recent version of Code lets you edit the settings.json via a GUI. Switch it to the JSON view mode. You can find that toggle button at the top right corner. "terminal.integrated.shell.linux": "bash" And, you're ...


8

You might be able to do something as simple as: #!/bin/bash targetDir="$1" while true; do files=("$targetDir"/*) sleep 30m ## wait 30 minutes files1=("$targetDir/"*) if [[ ${#files[@]} == ${#files1[@]} ]]; then echo "No new files added" | mail user@example.com fi done Save that script as ~/bin/watch.sh or whatever you like, make it ...


0

I managed to figure this out. If anyone is wanting to do the same, here is how to get it working. First install SSH pass. This allows you to pass a password to a remote SSH session In the terminal type the following: apt-get install sshpass This guide will walk you through setting up PPTP connections via the terminal. Once you have setup your VPN client ...


0

using option -t (terminal) in ssh command solves the problem too (better than redirecting stdout if command output is something usefull that we want to see): ssh -t remoteip command


21

You should be able to skip loading the default .bashrc file in WSL in essentially the same way as in "regular" bash: --norc Do not read and execute the system wide initialization file /etc/bash.bashrc and the personal initialization file ~/.bashrc if the shell is interactive. This option is on by default if the ...


12

GNU parallel is perfect for this sort of thing. Install with sudo apt install parallel and then: parallel -j0 ::: my_folder/*sh The -j controls how many processes t run in parallel, and -j0 means "all of them". Set it to another value (e.g. -j20) to run the scripts in batches instead.


1

To run all *.sh scripts in my_folder directory at the same time using xargs: $ ls my_folder/*.sh | xargs -P0 -n1 bash To limit the number of concurrently executed scripts to 10: $ ls my_folder/*.sh | xargs -P10 -n1 bash


3

There's a tool for this, read man run-parts. For example, I do: run-parts ${visorhome}/pbackup.d/ in my Palm Pilot backup script. ${visorhome}/pbackup.d/: 01PopulateJpilot 02Extract_Pedometer 03URLs 04google 05Books 06Weight 07Sec 08Bkgm 50hardlinks


8

To run all scripts at the same time (in parallel) use: script_1.sh & script_2.sh & script_3.sh & script_4.sh & script_5.sh & To run the one after the other (sequentially) use: script_1.sh && script_2.sh && script_3.sh && script_4.sh && script_5.sh Enhancement for comments If you have 200 scripts you ...


0

I am using Visual Studio Code and am new to the text editor. I was receiving the same error and tried manually following steps in this post, it did not work for me. However, in Visual Studio Code bottom right corner provides an option to switch between CR and LF on the fly, problem solved. I am not sure if this applies but if you are programming in a text ...


2

If you always want /opt/ros/kinetic/setup.bash sourced when you open a new interactive bash shell, put the source command at the end of the .bashrc file in your home directory. You may want to back it up (though if you haven't modified it then it's the same as /etc/skel/.bashrc). You can do by running: cp ~/.bashrc ~/.bashrc.orig Open ~/.bashrc in a text ...


Top 50 recent answers are included