shell,operating-system , why shell uses 0 as success?


why shell uses 0 as success?

Question:

Tag: shell,operating-system

In C and many other languages, 0 means false and 1 ( or non-zero ) means true. In the shell 0 for the status of a process means success and non-zero means an error. Shell if statements essentially use 0 for true. Why did the writer of the first shell decide to use 0 for true?


Answer:

The OP asking about the

Why did the writer of the first shell decide to use 0 for true?

The short answer: Isn't possible to answer this question in this form, because the first shell has not any exit status.

EDIT

Because @John Kugelman asked for summarisation, this answer getting a bit long.

Nor the second, third and Nth shell hasn't anything what could be called as exit status. For the approximative correct answer, need go to deeper into the history.

The shell was invented by "Louis Pouzin" as the "RUNCOM" tool, in the MIT's Compatible Time Sharing System read about CTSS, where is written:

Louis Pouzin also invented RUNCOM for CTSS. This facility, the direct ancestor of the Unix shell script, allowed users to create a file-system file of commands to be executed, with parameter substitution. Louis also produced a design for the Multics shell, ancestor of the Unix shell.

Read also this page.

The "next level" was in the Multics, caled as the Multics Command Language:

The command language interpreter, the Shell, is normally driven by the Listener.

Its function is to listen for requests in the form of command lines typed in at the user console. In the above command language description, the listener reads in a line from the console, evaluates the line as a command, and re-calls itself to repeat the function.

It hasn't anything what could be called as "exit status". The programs calls the terminate{file/process} procedure, what stops the program execution.

And this is one of the most important point. Need differentiate between

Back to the history:

After the Multics, the UNIX got developed. From this page the shell as we known todday has two predecessors:

If interested, read thru the manuals - links are here.

The first mentioning of the "exit code" is in the Manual of the PWB (aka Mashey) Shell. All shells "before" talks only about the:

Termination Reporting

If a command (not followed by "&") terminates abnormally, a message is printed. (All terminations other than exit and interrupt are considered abnormal.)

So, the answer to the question is in the above lines, - in line what is already said in the comments:

Nice citation from the PWB shell manual

$r is the exit status code of the preceding command. ``0'' is the normal return from most commands.

Notice the: most word. :), e.g. it was not invented in "one moment" by someone, but it is evolved in time. And it is very tightly depends on the implememtaion of the exit and wait-like calls in the underlaying OS.

For example, the 1st version of UNIX know nothing about the exit(N) levels - it was an simple exit() and the wait didn't return specific value - from the The first edition of the Unix Programmer's Manual, dated November 3, 1971

exit is the normal means of terminating a process. All files are closed and the parent process is notified if it is executing a wait.

wait causes its caller to delay until one of its child processes terminates. If any child has already died, return is immediate; if there are no children, return is immediate with the error bit set.

Also, read here - really worth - The Evolution of the Unix Time-sharing System - about the evolution of the fork, exec, exit and wait...

The V7 standardised the exit status as:

The value of a simple-command is its exit status if it terminates normally or 200+status if it terminates abnormally (see signal(2) for a list of status values).

also:

DIAGNOSTICS Errors detected by the shell, such as syntax errors cause the shell to return a non zero exit status. If the shell is being used non interactively then execution of the shell file is abandoned. Otherwise, the shell returns the exit status of the last command executed (see also exit).

Howg.. OMG... Someone could be so nice to edit this post and correct my broken english and/or extend/correct the above...

And finally - sorry folks - totally of topic - but can't resist to add the following image from the history - no many current users knows Microsoft-XENIX. ;) enter image description here


Related:


How to store command line arguments for future use?


bash,shell
I have a big bash script (version 3.2.51(1)-release) and I need to store command line arguments for use in the script. I checked many threads here and people suggested to use "[email protected]" to access all arguments. If I run this script (test.sh) - #!/bin/bash echo "[email protected]"; $> ./test.sh 1 2...

Linux-wget command


linux,shell,wget
I need a quick help on customizing my wget command in a shell script: The wget command looks something like this: wget http://infamvn:8081/nexus/content/groups/LDM_REPO_LIN64/com/infa/com.infa. products.ldm.ingestion.server.scala/10.0.0.135.527-SNAPSHOT/com.infa.products.ldm.ingestion.server.scala-10.0.0.135.527-20150622.210643-1-sources.jar Here I'd like to add the 10.0.0.135.527 in a variable, so I created a script something like this: n = 10.0.0.135.527 wget...

linux - running a process and tailing a file simultaneously


bash,shell,tail
I want a run a long task on a remote machine (with python fabric using ssh). It logs to a file on the remote machine. What I want to do is to run that script and tail (actively display) the log file content until the script execution ends. The problem...

docker run local script without host volumes


database,shell,docker,docker-compose
The goal is to add data to my database server containers of a multi-container web app from a download using curl once the database containers are running. I can do this from docker-compose.yml or from docker run independent of the web app, as long as I use host volumes. How...

Automate MySQL backup @localhost with mysqldump in Windows 8


mysql,windows,operating-system,scheduled-tasks,mysqldump
I'm trying to set up a task in Windows 8 to automate my localhost db dump. I've created the task to run daily with the following command line: C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe and parameters: --user=root --password=donttellya --result-file=dumped.sql mydb It works but doesn't terminate (running in the tasks list). Also, I'd...

Shell script to loop over files with same names but different extensions


linux,bash,shell
For example say I have: filename1.ext1 filename1.ext2 filename2.ext1 filename2.ext2 I need to write a shell script to feed these files into a program like so: program filename1.ext1 filename1.ext2 program filename2.ext1 filename2.ext2 Additionally the .ext1 files must be entered first and the .ext2 files second. Any help would be appreciated....

Allowing an executable to be terminated w/ ctrl+c in a shell script w/o exiting


bash,shell
Example: #!/bin/bash command_a # starting a executable command_b # should be executed after after exiting A A is exited via ctrl+C. I didn't really know how to search for this....

How to find average and maximum in an interval using Shell [closed]


linux,bash,shell,unix,awk
I would like to extract sum, mean and average in each 6 numbers interval from a column. I found many discussions related to this problem, but all those are for whole column. e.g. To compute sum of a column: awk '{sum+=$1} END { print sum}' To calculate Average: awk '{sum+=$1}...

Delete some lines from text using Linux command


linux,shell,sed,grep,pattern-matching
I know how to match text using regex patterns but not how to manipulate them. I have used grep to match and extract lines from a text file, but I want to remove those lines from the text. How can I achieve this without having to write a python or...

oh-my-zsh error after upgrade: ~/.oh-my-zsh/lib/misc.zsh:3: parse error near `then'


shell,zsh,zshrc,oh-my-zsh
today I upgrade my oh-my-zsh and then start this error: ~/.oh-my-zsh/lib/misc.zsh:3: parse error near `then' Inside this file there is this shell loop... ## Load smart urls if available for d in $fpath; do if [[ -e "$url/d-quote-magic"]]; then autoload -U url-quote-magic zle -N self-insert url-quote-magic fi done I think...

Using the shell provided with NodeJS


javascript,node.js,shell,require,node-modules
Once you've installed NodeJS, you'll have a executable in your computer named NodeJS which is a shell. I was wondering what can I do with that... here you're able to run JS code as, for example, in the browser's console, great. Now, is it possible to require modules in that...

shell script cut from variables


bash,shell,shellcode
The file is like this aaa&123 bbb&234 ccc&345 aaa&456 aaa$567 bbb&678 I want to output:(contain "aaa" and text after &) 123 456 I want to do in in shell script, Follow code be consider #!/bin/bash raw=$(grep 'aaa' 1.txt) var=$(cut -f2 -d"&" "$raw") echo $var It give me a error like...

find numbers divisible by 3 in csv file using shell script


bash,shell,unix,awk
I have csv file having content like below : 1|2|3 4|5|6 7|8|9 Now I would like to find the numbers which are divisible by 3 using shell scripting. I would like to use awk command for this. I am learning shell scripting. So could you please help me out to...

Use Unix Executable File to Run Shell Script and MPKG File


osx,shell,unix
I have 2 shell scripts and 2 mpkg installer, I am trying to use an unix excitable file to run them all. here is the script I have, but it always has error message "No such file or directory" ? #!/bin/sh # Find the absolute script current path path=$( cd...

Why does `su` not work in Jenkins?


shell,jenkins,su
su doesn't seem to work as intended when run in Jenkins build job script. su username1234 whoami returns jenkins My initial guess is that each line of the Jenkins shell script is a new shell. But doing repeated echo $$ on separate lines shows that's not the case. ...

How to extract single-/multiline regex-matching items from an unpredictably formatted file and put each one in a single line into output file?


linux,shell,unix,replace,grep
I have a very huge file which looks like this: <a>text</a>text blah <b>data1</b>abc<b>data2</b> <b>data3</b>blahblah <c>text</c> <d>text</d> <x>blahblah<b>data4 data5 data6</b> <b>data7 </x> That is, its formatting is unpredictable. I need to extract each <b>...</b> item (it might contain multiline text!) and put every one of them in a single separate line....

Missing one condition to display a string


shell
I am working on a small script that simply takes a parameter and read the output of ls -l and displays the user and name of the file that start with that parameter. For exemple : $> ls -l | ./script.sh "ok" John ok_file Mark ok_test Here is what the...

Matching string inside file and returning result


regex,string,bash,shell,grep
I've got a few peculiar issues with trying to search for a string inside of a .db file. The way I tried was by using grep, which does apparently find the string(s), although this is the output: $ grep "ext" *.db Binary file enormous.db matches There are a couple problems...

Sqoop Export with Missing Data


sql,postgresql,shell,hadoop,sqoop
I am trying to use Sqoop to export data from HDFS into Postgresql. However, I receive an error partially through the export that it can't parse the input. I manually went into the file I was exporting and saw that this row had two columns missing. I have tried a...

adb shell command from batch file


android,shell,adb
I have a .bat file that runs script for testing an app and printing the log to file i have all the commands i tested them manually. Problem: after entering the command adb shell, the shell opens in the command prompt. I wrote the next commands that are entered in...

how to deletes line from a text file that are taken from another file [duplicate]


shell,awk,sed,grep,sh
This question already has an answer here: Remove duplicates from text file based on second text file 4 answers I have a data.txt file with a lot of lines in it and a lines.txt that contains some lines. I want to delete all lines from data.txt that match any...

Check for decimal point and add it at the end if its not there using awk/perl


regex,perl,shell,awk
I have test.dat file with values given below: 20150202,abc,,,,3625.300000,,,,,-5,,,,,,,,,,,,,,,,,,,,,, 20150202,def,,,,32.585,,,,,0,,,,,,,,,,,,,,,,,,,,,, 20150202,xyz,,,,12,,,,,0.004167,,,,,,,,,,,,,,,,,,,,,, My expected output is shown below: 20150202,abc,,,,3625.300000,,,,,-5.,,,,,,,,,,,,,,,,,,,,,, ^. added here 20150202,def,,,,32.585,,,,,0.,,,,,,,,,,,,,,,,,,,,,, ^. added here 20150202,xyz,,,,12.,,,,,0.004167,,,,,,,,,,,,,,,,,,,,,, ^. added here So if column 6 and 11 doesn't have decimal point in it, then we should add '.' at the end of...

Why can I view some Unix executable files in Mac OS X and not others?


git,bash,shell,unix,binary
I am on a Macbook Pro on Mac OS X 10.10 (Yosemite). When I go to /usr/bin, git is there as a unix executable file. When I open it up in Sublime Text, all I get is unreadable machine code. However, when I open up a different Unix executable file—in...

Identifying when a file is changed- Bash


bash,shell,unix
So in my bash shell script, I have it running through a for loop. Inside the for loop, I use find "$myarray[i]" >> $tmp to look for a certain directory each time through the loop. Sometimes, it finds the variable in myarray[i] and sometimes it doesn't. When it does find...

Group of data in shell language


linux,bash,shell
I wonder if the data in the group aren’t numbers ; the script works? When x="1 2 3" I can do a for loop: for i in $x I wonder if this is correct : read x read y xm=`cat $x` ym=`cat $y` zm="$xm $ym" for i in $zm do...

AWK write to new column base on if else of other column


linux,bash,shell,awk,sed
I have a CSV file with columns A,B,C,D. Column D contains values on a scale of 0 to 1. I want to use AWK to write to a new column E base in values in column D. For example: if value in column D <0.7, value in column E =...

Using AWK to parse fields with commas


regex,bash,shell,awk
Edited - TLDR: Using awk to parse fields that include commas. # original config file - confile1 $ cat confile1 list=( app1,"HOSTNAME - port - application name - alert1",99.0,99.0 app2,"HOSTNAME - port - application name - alert1",99.0,99.0 app3,"HOSTNAME - port - service name - alert2",99.0,99.0 web1,"URL - HOSTNAMES(01,02) - http://someurl.com/...

Multiple line search in a file using java or unix command


java,shell,unix,command
I want to find the following pattern in a file like subclass "Pool1" 11:22:33:44:55:66 { dynamic; } the above pattern I have to find in a file. How can I find either using java or Unix command?...

using sed to replace a line with back slashes in a shell script


regex,bash,shell,ssh,sed
I am trying to replace the bottom one of these 2 lines with sed in a file. <rule>out_prefix=orderid ^1\\d\+ updatemtnotif/</rule>\n\ <rule>out_prefix=orderid ^2\\d\+ updatemtnotif/</rule>\n\ And the following command seems to do that when executed as a command at the bash prompt sed -i '[email protected]_prefix=orderid ^2\\\\d\\+ updatemtnotif/@out_prefix=orderid ^2\\\\d\\+ updatemtnotif_fr/@g' /opt/temp/rules.txt however, when...

how to spy on linux binaries for testing of shell scripts


linux,bash,shell,testing,spy
sorry if this is a duplicate, i had no idea what to search for... my use case is more complex, but can be narrowed down to the following problem: i want to run a bash script, which invokes all sorts of binaries, for example: grep. i want to assert that...

Not Used Recently (NUR) Page Replacement Algorithm [closed]


java,c++,c,operating-system
How to implement NUR (Not used recently page replacement algorithm) using any high level programming language (c, c++ or java)

shell script to shutdown/restart Linux system


linux,shell
Is there any suitable shell script for shutting down or restarting a Linux machine? I have tried a shell script for shutdown, but when I enter sudo shutdown it will ask for the password. How we can enter the password using the script?

Shell script file, checks if text file is empty or not


shell,text-files
I want to check if .todo.txt is empty or not. If is is empty, I want to add the echo in the file, and if it already has content, I want to do something else. I tried: hasData() { echo "Hello" } isEmpty() { echo -e "$n\t$comment\t$dueD/$dueM/$dueY " >> .todo.txt...

Piping echo into sendmail in rc.local fails


linux,shell,email,sendmail,boot
I'm trying to get my system to send me an email when the system boots. Using the following command works as intended: echo -e "TO:[email protected] \nSUBJECT:System Booted \n\nBeware I live! \n"$1 | sendmail -t -vs But if I add it to rc.local like so: #!/bin/sh -e # # rc.local #...

Export database from a remote server into a specific folder on my computer


php,mysql,database,shell,command-line-interface
I'm having trouble with a database export that is too big for phpmyadmin to handle. I'm curious what the syntax would be for exporting a mysql database from a remote server and getting the export file into a specific folder on my own computer, all using the command line. Say...

Reading tab (\t) separated text from file in shell


bash,shell,text-files
my text file looks like this 1 get a 06-05-2000 2 get b 06-05-2001 3 get c 06-05-2002 4 get d 06-05-1442 5 get e 06-05-1998 6 get f 06-05-1909 I want to read what is after the first \t (tab) and store that in varA and what is after...

How to test if a command is a shell reserved word?


bash,shell
I am writing a bash script and I would like to verify if a string is a shell reserved word (like if, for, alias, etc...). How can I can do this?...

Calling find more than once on the same folder tree


linux,bash,shell,unix,find
I'm running a find command multiple times on the same group of files. The results of my find commands are usually disjoint sets, AKA I'm running find -mmin +35; find -mmin -25, and doing different things to the results. It seems sort of silly to search through the entire file...

NSUserDefaults not working on Xcode beta with Watch OS2


swift,operating-system,nsuserdefaults,watchkit
I just installed the latest beta of Xcode to try Swift 2 and the improvements made to the Apple Watch development section. I'm actually having an hard time figuring out WHY this basic NSUserDefaults method to share informations between iOS and Watch OS2 isn't working. I followed this step-by-step tutorial...

AWK count number of times a term appear with respect to other columns


linux,shell,command-line,awk,sed
Given a CSV file: id, fruit, binary 1, apple, 1 2, orange, 0 3, pear, 1 4, apple, 0 5, peach, 0 6, apple, 1 How can i calculate for each unique values in fruit, the number of times the binary value =1 / number of occurences of that fruit...

Shell script to check if mysql is up or down


mysql,shell,cron
I want a bash shell script that i can run using a cron job to check if mysql on a remote server is running. If it is, then do nothing, other start the server. The cronjob will be checking the remote server for a live (or not) mysql every minute....

Redirect output from file to stdout


bash,shell,unix,stdout
I have a program that can output its results only to files, with -o option. This time I need to output it to console, i.e. stdout. Here is my first try: myprog -o /dev/stdout input_file But it says: /dev/ not writable I've found this question that's similar to mine, but...

How to extract first letters of dashed separated words in a bash variable?


linux,string,bash,shell,variables
I would like to extract the first letter of dashed separated words value of my bash variable, like this: MY_TEXT=this-is-my-custom-text I would like to create a second variable like this: MY_INITIALS=timct...

How do I align my output?


shell
I'm writing a script that takes a parameter and reads the output of ls -l. It then displays the user and the name of the file STARTING with that parameter. For example : $> ls -l | ./script.sh "o" John ok_test RandomUser o_file My script works just fine, but I...

Storing data in shell


shell,sh
I have a need to store a list of e-mails in a shell script. This script will get called and passed a customer number. depending on the customer number I want to populate a variable based on the passed in customer number. I am not sure how to accomplish this...

How do I silence the HEAD of a curl request while using the silent flag?


bash,shell,curl,command-line,pipe
When I run the curl command and direct the data to a file, I get back the content of the site as expected. $ curl "www.site.com" > file.txt $ head file.txt Top of site ... However, this command shows a progress bar, which I do not want: % Total %...

Fail Jenkins job when nosetests fail


python,shell,jenkins,continuous-integration
I am managing deployment / CI of a Flask app through Jenkins. I have a build step in the form of an executed shell which runs a shell script in the host that in turns runs nosetests. The Jenkins shell command looks like so: $WORKSPACE/ops/test_integration.sh integration.sh looks like so: SETTINGS_FILE="settings/integration.py"...

Mac OSX - Allow for user input in shell script via GUI or Prompt


osx,bash,shell
first time post so please let me know how I could improve.. I created a shell script which requires a person to input their name and then generates a report. The script works as needed when chmod'd into an executable script and run from terminal. But, I would like to...

How to replace newlines/linebreaks with a single space, but only if they are inside of start/end regexes?


regex,linux,shell,unix,replace
I have an extremely big (many GBs in size) file that looks like [x data1 data2 data3 data4 y] [a data5 data 6 data7 data 8 b> [x data y] ...and so on How to replace newlines (which might be surrounded by spaces) with a single space, but only if...

shell script to read each line and run the condition


shell
I have a log file which has the below data 120 140 200 110 200 200 120 90 100 I want to read this file and compare each line(number) with 200, and if it crosses 200 - then it has to compare the next word till the 5 consecutive which...