sh

What's a .sh file?

三世轮回 提交于 2019-11-29 18:51:29
So I am not experienced in dealing with a plethora of file types, and I haven't been able to find much info on exactly what .sh files are. Here's what I'm trying to do: I'm trying to download map data sets which are arranged in tiles that can be downloaded individually: http://daymet.ornl.gov/gridded In order to download a range of tiles at once, they say to download their script, which eventually leads to daymet-nc-retrieval.sh : https://github.com/daymet/scripts/blob/master/Bash/daymet-nc-retrieval.sh So, what exactly am I supposed to do with this code? The website doesn't provide further

Shell read *sometimes* strips trailing delimiter

纵饮孤独 提交于 2019-11-29 18:09:57
问题 To parse colon-delimited fields I can use read with a custom IFS : $ echo 'foo.c:41:switch (color) {' | { IFS=: read file line text && echo "$file | $line | $text"; } foo.c | 41 | switch (color) { If the last field contains colons, no problem, the colons are retained. $ echo 'foo.c:42:case RED: //alert' | { IFS=: read file line text && echo "$file | $line | $text"; } foo.c | 42 | case RED: //alert A trailing delimiter is also retained... $ echo 'foo.c:42:case RED: //alert:' | { IFS=: read

Using 'find' to return filenames without extension

余生长醉 提交于 2019-11-29 17:50:21
问题 I have a directory (with subdirectories), of which I want to find all files that have a ".ipynb" extension. But I want the 'find' command to just return me these filenames without the extension. I know the first part: find . -type f -iname "*.ipynb" -print But how do I then get the names without the "ipynb" extension? Any replies greatly appreciated... 回答1: To return only filenames without the extension, try: find . -name "*.ipynb" -execdir sh -c 'printf "%s\n" "${0%.*}"' {} ';' or: find "

How can you use pure unset shell builtin? Can you write shell scripts that are immune to tampering?

萝らか妹 提交于 2019-11-29 16:12:22
问题 I mean I want to use unset that is not a shell function itself. If I could do that, I could make sure that command is pure by running #!/bin/sh { \unset -f unalias command [; \unalias unset command [ } 2>/dev/null; # make zsh find *builtins* with `command` too: [ -n "$ZSH_VERSION" ] && options[POSIX_BUILTINS]=on If I am using Debian Almquist shell (dash), I think I can rely that \unset is pure. At least I could not define a shell function named unset in dash . Whereas in bash or in zsh I

Print stdout in Python without shell escape sequences

大兔子大兔子 提交于 2019-11-29 16:07:57
I'm using sh to run git commands inside a Python script. In [1]: from sh import git In [2]: s = git("log", "-1", pretty="format:%h %s") In [3]: print s 4f14a66 basic debug page This seems to work as expected. However, using this in a Django template gives [?1h= 4f14a66 basic debug page[m [K[?1l> . I tried to see what characters were in this string using repr() , to no avail: In [4]: print repr(s) 4f14a66 basic debug page It turns out commands in sh return a RunningCommand that has a .stdout attribute: In [5]: type(s) Out[5]: sh.RunningCommand In [7]: s.stdout Out[7]: '\x1b[?1h\x1b=\r4f14a66

Running Executable from Shell Script

青春壹個敷衍的年華 提交于 2019-11-29 14:31:18
I am having trouble launching an executable that I have created from a shell script. I would like to automate testing by running the program many times with different command line options to verify it is working. When I type echo $SHELL , /bin/sh is displayed. The following is my shell script: #!/bin/sh clear echo "Running first test." ./myProgram exit 0 When I run the script ( sh myScript.sh ), with myProgram in the same directory, I see the following output: Running first test. : not foundsh: line 4: When executing the program ./myProgram , it runs as expected with no command line options. I

Git post-commit hook as a background task

不打扰是莪最后的温柔 提交于 2019-11-29 13:22:48
I have a script, that I need to run after committing to a project under git revision control. Therefore I created a post-commit hook in my projects .git directory in the subdirectory /hooks, named it 'post-commit' and gave it the following contents: #!/bin/sh # I am a post-commit hook /usr/local/bin/my_script & my_script is executable and runs fine in /bin/sh. In fact it has a runtime of several seconds, so I want it to be backgrounded and detached from the current shell. That's why I put the trailing '&' to my hook. The problem now is, that the '&' seems to be ignored. When I commit using

Portable way to check emptyness of a shell variable [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-11-29 13:18:48
This question already has an answer here: Why do shell script comparisons often use x$VAR = xyes? 7 answers What is the portable and canonical way to test if variable is empty/undefined in a shell script? It should work in all sh-like shells. What I do now is something like: if [ -z "$var" ] ; then ... and for reverse, doing something when variable is not empty/undefined: if [ -n "$var" ] ; then ... And while these work for the scripts I write now, I'd like to know a way, that will work in any reasonably compatible sh -like shell , even on some more obscure environment than a Linux PC with GNU

difference between sh and bash when symlink is used

寵の児 提交于 2019-11-29 11:30:06
I have a shell script which uses process substitution The script is: #!/bin/bash while read line do echo "$line" done < <( grep "^abcd$" file.txt ) When I run the script using sh file.sh I get the following output $sh file.sh file.sh: line 5: syntax error near unexpected token `<' file.sh: line 5: `done < <( grep "^abcd$" file.txt )' When I run the script using bash file.sh , the script works. Interestingly, sh is a soft-link mapped to /bin/bash . $ which bash /bin/bash $ which sh /usr/bin/sh $ ls -l /usr/bin/sh lrwxrwxrwx 1 root root 9 Jul 23 2012 /usr/bin/sh -> /bin/bash $ ls -l /bin/bash

What does “$?” give us exactly in a shell script? [duplicate]

两盒软妹~` 提交于 2019-11-29 10:53:53
问题 This question already has an answer here: Meaning of $? (dollar question mark) in shell scripts 8 answers I saw the code written somewhere online, and I wanted to know what exactly does "$?" do/give us. Googling did not help. Here's the code I saw it in: #!/bin/sh ping -c 2 localhost if [ $? != 0 ] ; then echo "Couldn't ping localhost, weird" fi ping -c 2 veryweirdhostname.noend if [ $? != 0 ] ; then echo "Surprise, Couldn't ping a very weird hostname.." fi echo "The pid of this process is $$