I have the following bash script (myscript.sh): Now: before sourcing myscript.sh, $SHLVL is 2. After sourcing it, is 3. Why? Shouldn’t the command evaluation exit after echo? Thank you! Answer When you source a script in bash, $0 expands to bash (or at least, the name of the shell bash was started as). …
Tag: bash
Moves files from directory by reading file names from a text file
I want a script that is able to read the content of a text file which contains folder names and moves the folders from their directory to a specific folder. Here is my script: This script is partly working as it copies only the last folder in the text file, as for the other folders it gives the error “n…
list files that start with two dots and change file names in bulk
I’ve got file that start with two dots located in different directories. I need to list them all and change their names in bulk and remove the dots completely. Any suggestions how to do that? Answer Not very efficient, due to the sh invocation for each file, but this should work, and is safe: How it wor…
Save File With Shell Script Content
$net_script is variable and stored text (script content). I need to create run.sh with $net_script content. $net_script: But it’s not stored correctly. I tried echo but that didn’t work. Answer Try this
How can i give specific commands to programs via terminal Linux?
I’m seeking for a way to automatize the configuration of some tasks i do, but, i am having some trouble to give the order to the programs from the terminal. I have actually to give, every time i try to configure some Routers, the same commands always and always, but, i’m looking for a way to creat…
Using bash, how can I remove the extensions of all files in a specific directory?
I want to keep the files but remove their extensions. The files do not have the same extension to them. My end goal is to remove all their extensions and change them to one single extension of my choice. I have the second part down. My code so far: Answer You don’t need an external command find for this…
Replace text between two lines with contents of a file stored in a variable in sed
Let’s say I have a file called original.txt with this content: red blue water food tree gray white Also I have a file called new.txt with this content: green black yellow purple Now I want to write a script that replaces the lines between blue and gray in original.txt with the contents of new.txt, so it…
escape one variable but not the other in awk
I’m facing a problem with awk in Linux. I would like to do make this script work : The problem here is that I want the variable “var” to be interpreted (it works) and the variable $OTHERVAR not to be interpreted, and this I what I can’t manage to do. In the end, I want to do this: I ha…
Compare different columns of subsequent rows to merge ranges
I have a list of ranges, and I am trying to merge subsequent entries which lie within a given distance of each other. In my data, the first column contains the lower bound of the range and the second column contains the upper bound. The logic follows: if the value in column 1 is less than or equal to the
How to redirect entire output of spark-submit to a file
So, I am trying to redirect the output of an apache spark-submit command to text file but some output fails to populate file. Here is the command I am using: I can see the output in the terminal but I do not see it in the file. What am I forgetting or doing wrong here? Edit: If I use I