scripting

Wait for bash background jobs in script to be finished

余生长醉 提交于 2019-12-28 05:30:47
问题 To maximize CPU usage (I run things on a Debian Lenny in EC2) I have a simple script to launch jobs in parallel: #!/bin/bash for i in apache-200901*.log; do echo "Processing $i ..."; do_something_important; done & for i in apache-200902*.log; do echo "Processing $i ..."; do_something_important; done & for i in apache-200903*.log; do echo "Processing $i ..."; do_something_important; done & for i in apache-200904*.log; do echo "Processing $i ..."; do_something_important; done & ... I'm quite

Whats the difference between running a shell script as ./script.sh and sh script.sh

假装没事ソ 提交于 2019-12-28 05:11:09
问题 I have a script that looks like this #!/bin/bash function something() { echo "hello world!!" } something | tee logfile I have set the execute permission on this file and when I try running the file like this $./script.sh it runs perfectly fine, but when I run it on the command line like this $sh script.sh It throws up an error. Why does this happen and what are the ways in which I can fix this. 回答1: Running it as ./script.sh will make the kernel read the first line (the shebang), and then

Batch Script Issue - How do I convert the dates or suggest any logic to satisfy the IF statement requirements in the given code?

雨燕双飞 提交于 2019-12-28 04:36:05
问题 I have this already initialized batch script codes running in bat file: @echo off setlocal EnableExtensions DisableDelayedExpansion rem // Define constants here: set "_ROOT=%~dp0." & rem // (common root directory; `%~dp0.` is script's parent, `.` is current) set "_DIR1=%_ROOT%\Directory1" & rem // (1st directory containing files) set "_DIR2=%_ROOT%\Directory2" & rem // (2nd directory containing files) set _MASKS="*INV*." "*SLS*." & rem // (list of quoted file masks) set "_TMP1=%TEMP%\%~n0_1_

Running a Bash script over ssh

人盡茶涼 提交于 2019-12-28 02:35:31
问题 I'm trying to write a Bash script that will SSH into a machine and create a directory. The long-term goal is a bit more complicated, but for now I'm starting simple. However, as simple as it is, I can't quite seem to get it. Here's my code: #!/bin/bash ssh -T tunneluser@111.222.333.444 <<EOI # Fix "TERM environment variable undefined" error. TERM=dumb export TERM # Store todays date. NOW=$(date +"%F") echo $NOW # Store backup path. BACKUP="/backup/$NOW" [ ! -d $BACKUP ] && mkdir -p ${BACKUP}

How to add timestamp to STDERR redirection

孤人 提交于 2019-12-27 17:01:26
问题 In bash/ksh can we add timestamp to STDERR redirection? E.g. myscript.sh 2> error.log I want to get a timestamp written on the log too. 回答1: If you're talking about an up-to-date timestamp on each line, that's something you'd probably want to do in your actual script (but see below for a nifty solution if you have no power to change it). If you just want a marker date on its own line before your script starts writing, I'd use: ( date 1>&2 ; myscript.sh ) 2>error.log What you need is a trick

How to add timestamp to STDERR redirection

﹥>﹥吖頭↗ 提交于 2019-12-27 17:01:12
问题 In bash/ksh can we add timestamp to STDERR redirection? E.g. myscript.sh 2> error.log I want to get a timestamp written on the log too. 回答1: If you're talking about an up-to-date timestamp on each line, that's something you'd probably want to do in your actual script (but see below for a nifty solution if you have no power to change it). If you just want a marker date on its own line before your script starts writing, I'd use: ( date 1>&2 ; myscript.sh ) 2>error.log What you need is a trick

Setting a windows batch file variable to the day of the week

醉酒当歌 提交于 2019-12-27 14:57:05
问题 I have a windows batch file that runs daily. Wish to log data into a file and want to rotate it (i.e. having at most the last 7 days worth of data). Looked into the commands DATE and DELIMS - Cannot figure out a solution. Is there a simple solution to create a file name that contains the day of the week i.e. 0 for monday etc. Or do I have to resort to some better shell script. 回答1: %DATE% is not your friend. Because the %DATE% environment variable (and the DATE command) returns the current

What is the $? (dollar question mark) variable in shell scripting? [duplicate]

若如初见. 提交于 2019-12-27 12:40:12
问题 This question already has answers here : Meaning of $? (dollar question mark) in shell scripts (8 answers) Closed last year . I'm trying to learn shell scripting, and I need to understand someone else's code. What is the $? variable hold? I can't Google search the answer because they block punctuation characters. 回答1: $? is used to find the return value of the last executed command. Try the following in the shell: ls somefile echo $? If somefile exists (regardless whether it is a file or

What is the $? (dollar question mark) variable in shell scripting? [duplicate]

我的梦境 提交于 2019-12-27 12:40:07
问题 This question already has answers here : Meaning of $? (dollar question mark) in shell scripts (8 answers) Closed last year . I'm trying to learn shell scripting, and I need to understand someone else's code. What is the $? variable hold? I can't Google search the answer because they block punctuation characters. 回答1: $? is used to find the return value of the last executed command. Try the following in the shell: ls somefile echo $? If somefile exists (regardless whether it is a file or

What is the $? (dollar question mark) variable in shell scripting? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-27 12:39:09
问题 This question already has answers here : Meaning of $? (dollar question mark) in shell scripts (8 answers) Closed last year . I'm trying to learn shell scripting, and I need to understand someone else's code. What is the $? variable hold? I can't Google search the answer because they block punctuation characters. 回答1: $? is used to find the return value of the last executed command. Try the following in the shell: ls somefile echo $? If somefile exists (regardless whether it is a file or