Is It a Good Way to Use Cd and Cd - Commands in a Shell Script for Some Computation in Another Directory

Is it a good way to use cd and cd - commands in a shell script for some computation in another directory

Put it in a subshell:

(cd "$input_dir" && exec awk '$1 > 99 {printf "%.2f" "$1"}' ifile.txt > ofile.tx)

Thus, when the subshell exits, you're automatically back to your original directory, because the cd only applied to that subshell (containing only the awk command).

The exec ensures that you're not incurring extra overhead, as it causes the subshell to replace its process table entry with the awk. (Some shells will do this automatically for the last command inside a subshell).

How best to include other scripts?

I tend to make my scripts all be relative to one another.
That way I can use dirname:

#!/bin/sh

my_dir="$(dirname "$0")"

"$my_dir/other_script.sh"

How to cd between directories using a python script

To change working directory of use

os.chdir("/your/path/here")

subprocess will spawn new process and this doesn't affect your parent.

Bash: use cd command on a variable including spaces

Why don't you just...

cd "$input_path"

since it is quoted, there won't be any problem with spaces.

By saying cd $(echo "$input_path") you are in fact saying cd my path, whereas you want to do cd "my path". Thus, as commented by JID below, you can also say cd "$(echo $input_path)" because the important quotes are the ones that are "closer" to cd.


If you don't quote, cd sees:

cd my path

So it tries to cd my, whereas if you quote it sees:

cd "my path"

How to run 'cd' in shell script and stay there after script finishes?

You need to source the file as:

. myfile.sh

or

source myfile.sh

Without sourcing the changes will happen in the sub-shell and not in the parent shell which is invoking the script. But when you source a file the lines in the file are executed as if they were typed at the command line.

Get Folder Size from Windows Command Line

You can just add up sizes recursively (the following is a batch file):

@echo off
set size=0
for /r %%x in (folder\*) do set /a size+=%%~zx
echo %size% Bytes

However, this has several problems because cmd is limited to 32-bit signed integer arithmetic. So it will get sizes above 2 GiB wrong1. Furthermore it will likely count symlinks and junctions multiple times so it's at best an upper bound, not the true size (you'll have that problem with any tool, though).

An alternative is PowerShell:

Get-ChildItem -Recurse | Measure-Object -Sum Length

or shorter:

ls -r | measure -sum Length

If you want it prettier:

switch((ls -r|measure -sum Length).Sum) {
{$_ -gt 1GB} {
'{0:0.0} GiB' -f ($_/1GB)
break
}
{$_ -gt 1MB} {
'{0:0.0} MiB' -f ($_/1MB)
break
}
{$_ -gt 1KB} {
'{0:0.0} KiB' -f ($_/1KB)
break
}
default { "$_ bytes" }
}

You can use this directly from cmd:

powershell -noprofile -command "ls -r|measure -sum Length"

1 I do have a partially-finished bignum library in batch files somewhere which at least gets arbitrary-precision integer addition right. I should really release it, I guess :-)

Running a command as Administrator using PowerShell?

If the current console is not elevated and the operation you're trying to do requires elevated privileges then you can start powershell with the Run as Administrator option :

PS> Start-Process powershell -Verb runAs

Microsoft Docs: Start-Process

How to run 'sudo' command in windows

There is no sudo command in Windows. The nearest equivalent is "run as administrator."

You can do this using the runas command with an administrator trust-level, or by right-clicking the program in the UI and choosing "run as administrator."



Related Topics



Leave a reply



Submit