scripting

KornShell Printf - Padding a string

妖精的绣舞 提交于 2019-12-23 19:32:21
问题 I'm attempting to write a KornShell (ksh) function that uses printf to pad a string to a certain width. Examples: Call padSpaces Hello 10 Output 'Hello ' I currently have: padSpaces(){ WIDTH=$2 FORMAT="%-${WIDTH}.${WIDTH}s" printf $FORMAT $1 } Edit: This seems to be working, in and of itself, but when I assign this in the script it seems to lose all but the first space. TEXT=`padSpaces "TEST" 10` TEXT="${TEXT}A" echo ${TEXT} Output: TEST A I'm also open to suggestions that don't use printf.

why do `#!/usr/bin/env var=val command` gets into an infinite loop

試著忘記壹切 提交于 2019-12-23 18:29:53
问题 In man(1) env it say: env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...] So consider print_A.sh : #!/usr/bin/env A=b bash echo A is $A When I run it with ./print_A.sh it hangs. Running it with strace ./print_A.sh I get the following log, repeating: execve("/path/to/print_A.sh", ["/path/to/print_A.sh"...], [/* 114 vars */]) = 0 uname({sys="Linux", node="my-host", ...}) = 0 brk(0) = 0x504000 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2a95556000 access("/etc

Why does my bash function return the wrong value?

泪湿孤枕 提交于 2019-12-23 18:15:32
问题 fact() { if [ $1 -eq 1 ] then return 1 else y=`expr $1 - 1` fact $y b=$(($1 * $?)) return $b fi } echo "enter" read n fact $n echo "$?" This is a program to find the factorial of a number. The output is correct up to 5. The output of 6 is giving as 208 but the correct answer is 720. What's causing this error? 回答1: Function return values can only go up to 255: a() { return 255 } a echo $? b() { return 256 } b echo $? Produces: $ bash x.sh 255 0 return is like exit and exit can only take values

unix - cut command (adding own delimiter)

此生再无相见时 提交于 2019-12-23 17:56:50
问题 Given a file with data like this (ie stores.dat file) id storeNo type 2ttfgdhdfgh 1gfdkl-28 kgdl 9dhfdhfdfh 2t-33gdm dgjkfndkgf Desired output: id |storeNo |type 2ttfgdhdfgh |1gfdkl-28 |kgdl 9dhfdhfdfh |2t-33gdm |dgjkfndkgf Would like to add a "|" delimiter between each of these 3 cut ranges: cut -c1-18,19-30,31-40 stores.dat What is the syntax to insert a delimiter between each cut? BONUS pts (if you can provide the option to trim the values like so): id|storeNo|type 2ttfgdhdfgh|1gfdkl-28

Powershell script to run another powershell script and wait till over

我是研究僧i 提交于 2019-12-23 17:48:21
问题 i'm trying to run a script which will run another powershell script. i need the first script to continue only after the other one is over. something like: start Copy.ps1 wait till Copy.ps1 is done continue with the script tried using the Invoke-Expression but it doesn't have wait parameter. 回答1: I think you easily can do it by calling the file like this: <# here is some awesome code #> // Call P$ Script here .("C:\Path\To\Copy.ps1") <# awesome code continues here #> it will call the whole

How can a script distinguish Docker Toolbox and Docker for Windows?

不想你离开。 提交于 2019-12-23 17:29:05
问题 On my current team, we're still transitioning from Docker Toolbox to Docker Desktop for Windows . A lot of our scripts still assume that you're running Docker Toolbox on VirtualBox (like how to mount drives, how slashes or drive names work for those mounts). Is there a reliable way to tell, from inside a script, whether docker is coming from Docker Toolbox or Docker Desktop for Windows? 回答1: Toolbox works via docker-machine . The way the docker client is directed to the virtual machine is via

Passing command line arguments through Bash

大城市里の小女人 提交于 2019-12-23 17:28:00
问题 While brushing up on bash (it's been a while), I was surprised to notice that executing this code, saved as script.sh: echo "Arg 0 to script.sh: $0" echo "Arg 1 to script.sh: $1" function echo_args { echo "Arg 0 to echo_args: $0" echo "Arg 1 to echo_args: $1" } echo_args like this: >> ./script.sh argument output this: Arg 0 to script.sh: ./script.sh Arg 1 to script.sh: argument Arg 0 to echo_args: ./script.sh Arg 1 to echo_args: I was surprised to see that $0 of script.sh was passed as $0 to

How to delete all empty folders which are older than 2 days?

岁酱吖の 提交于 2019-12-23 17:05:08
问题 I make a Script which deletes all empty Folders with Subfolders in a Path. Now i have to make, if a folder was created 2 days ago and its empty it should be deleted with the other empty folders that are older than 2 Days.And if not it should be not deleted. And i also need/want to make that deleted Folder are written in a Log. I made that with the 5 Filetypes but i dont know how this schould work with the Folders. Im really new to Batch, so i dont know what i should do. I checked Google but

How to read n lines above the matched string in perl?

不羁岁月 提交于 2019-12-23 14:23:57
问题 Say I have a file xx.txt and it contains the data 1 I am here 2 to work in 3 Perl for writing 4 a script for 5 myself Suppose i want to search for string script and want to display the three lines above it , what should i do ? 回答1: You can use an array as your print buffer. my @array; while (<DATA>) { push @array, $_; shift @array if @array > 4; print @array if /script/; } __DATA__ 1 I am here 2 to work in 3 Perl for writing 4 a script for 5 myself Various usage: If you intend to use this as

How to read n lines above the matched string in perl?

删除回忆录丶 提交于 2019-12-23 14:23:13
问题 Say I have a file xx.txt and it contains the data 1 I am here 2 to work in 3 Perl for writing 4 a script for 5 myself Suppose i want to search for string script and want to display the three lines above it , what should i do ? 回答1: You can use an array as your print buffer. my @array; while (<DATA>) { push @array, $_; shift @array if @array > 4; print @array if /script/; } __DATA__ 1 I am here 2 to work in 3 Perl for writing 4 a script for 5 myself Various usage: If you intend to use this as