Using same in linux

Do all different Linux distributions have the same command lines? [closed]

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Do all different Linux distributions have the same command lines? What I want to know is the same command line works for all kinds of Linux distributions (CentOS, Fedora, Ubuntu, etc.) or whether they all have different command lines?

This is a hard-to-find-on-internet question, just like any beginner-level questions, asked with some misunderstanding about general level system structure/layers. It’s like no community welcome such questions, causing too much pain for beginners.

3 Answers 3

I’m choosing to interpret this question as a question about the portability of commands and shells across various Linux distributions. A «command line» could mean both «a command written at the shell prompt» and «the shell itself». Hopefully this answer addresses both these interpretations of «command line».

Most Unix systems provide the same basic utilities for working at the shell prompt. These utilities are working largely in the same way since they are standardised. Also, the syntax used for writing shell commands is standardised (loops, redirections, pipes, background processes, variable assignments, quoting etc.) The standard is called POSIX and may be found here (see the «Shell & Utilities» section).

On most Unices (especially on Linux for some reason), the standard utilities have been extended with extra functionality, but the functionality described by the POSIX standard should be implemented. If a standard utility does not conform to the POSIX standard, you should probably file a bug report about this.

In particular, the shell itself is often extended to give a more convenient interactive experience, or to be able to provide more advanced shell programming facilities. The shell, being an application like any other, comes in various flavours (implementations) and bash is the most popular on Linux systems (but it’s also available as the default shell on e.g. macOS and may be installed on any Unix). The zsh and ksh shells are also popular and provide different sets of extensions, but all should at least be able to do largely what the POSIX standard says using a common syntax (except when using extensions such as special types of arrays and fancier forms of filename pattern matching etc. although some of this happens to be fairly similar between shells too).

Читайте также:  Узнать прокси на линукс

As for non-standard tools, such as tools for doing some specific task that is not covered by the POSIX standard (such as talking to a database or adjusting the brightness level of a monitor), or that are specific to a particular Linux distribution (maybe for doing package management), to a version of a particular Linux distribution, or to a particular hardware architecture etc., the portability of the command would depend on the correct variant and version of the tool being installed on a system that supports using that tool.

Across various Linux distributions, the assortment of available tools and utilities is fairly homogenous, and portability is generally good (with the caveat that distribution and architecture specific tools may be different or missing). When looking at using and writing scripts that should work on other types of Unix systems, it becomes more important to know about what extensions are particular to the GNU/Linux variation of tools and utilities, and what can be expected to work on «a generic POSIX/Unix system».

Источник

xargs : using same argument in multiple commands

Am trying to write a one-liner that can probe the output of df -h and alert when one of the partitions is out [or almost] of space. It’s the part using xargs that kicking me in the ass now.

echo 95 | xargs -n1 -I<> [ <> -ge 95 ] && echo "No Space on disk <>% full -- remove old backups please" 

3 Answers 3

That && is not part of the xargs command, it’s a completely separate invocation. I think you’ll want to execute a shell explicitly:

echo 95 | xargs -I_percent sh -c '[ "$1" -ge 95 ] && echo "No Space on disk $1% full -- remove old backups please"' sh _percent 

Note also that I’m using _percent instead of <> to avoid extra quoting headaches with the shell. It’s not a shell variable; still just an xargs replacement string.

Читайте также:  Installing debian package on linux

An alternative way, which is more readable, is to define a separate function which contains all your other commands and then call that function with xargs in a sub-shell.

myfunc() < [ "$1" -ge 95 ] && echo "No Space on disk $1% full -- remove old backups please" echo "Another command echoing $1" >export -f myfunc echo 95 | xargs -n1 -I_percent -- sh -c 'myfunc "_percent"' 

You can just use the sh option mentioned and enclose the command in single quotes as show below with an extra equivalent if version:

#Your example echo 95 | xargs -n1 -I<> sh -c '[ <> -ge 95 ] && echo "No Space on disk <>% full -- remove old backups please"' #if equivalent echo 95 | xargs -n1 -I<> sh -c 'if [ <> -ge 95 ]; then echo "No Space on disk <>% full -- remove old backups please"; fi' 

Using xargs with alias in -I option for better readability

echo 95 | xargs -n1 -IusedSpace sh -c 'if [ usedSpace -ge 95 ]; then echo "No Space on disk usedSpace% full -- remove old backups please"; fi' 

With if and chain as many command as You wish separated by semicolon ; below example with 2 and 4 commands chained

# 2 independent commands echo 95 | xargs -n1 -I<> sh -c 'if [ <> -ge 95 ]; then echo "No Space on disk <>% full -- remove old backups please"; echo "Value processed <>"; fi' # 4 independent commands echo 95 | xargs -n1 -I<> sh -c 'if [ <> -ge 95 ]; then echo "No Space on disk <>% full -- remove old backups please"; echo "Space left on disk $( expr 100 - <>)%"; echo "Space used <>%"; echo "Value processed <>"; fi' 

I added the else case in your example, It prints the left disk space

#If with else block echo 80 | xargs -n1 -I<> sh -c 'if [ <> -ge 95 ]; then echo "No Space on disk <>% full -- remove old backups please"; else echo "Space left on disk $( expr 100 - <>)%"; fi' #Equivalent echo 80 | xargs -n1 -I<> sh -c '[ <> -ge 95 ] && echo "No Space on disk <>% full -- remove old backups please"; [ <> -ge 95 ] || echo "Space left on disk $( expr 100 - <>)%"' 

Another modification could be isolate the 95 percent of limit usage in a variable as show below:

echo 96 | xargs -n1 -IusedSpace sh -c 'usageLimit=95; if [ usedSpace -ge $ ]; then echo "No Space on disk usedSpace% full -- remove old backups please"; else echo "Space left on disk $( expr 100 - usedSpace)%"; fi' 

Источник

Читайте также:  Astra linux резервная копия системы

How to avoid ‘are the same file’ warning message when using cp in Linux?

The problem is that you try to copy a file to itself. You can avoid it by excluding the destination directory from the results of the find command like this:

find "$HOME" -name '*.txt' -type f -not -path "$HOME/newdir/*" -print0 | xargs -0 cp -t "$HOME/newdir" 

try using install instead, this replaces by removing the file first.

install -v target/release/dynnsd-client target/ removed 'target/dynnsd-client' 'target/release/dynnsd-client' -> 'target/dynnsd-client' 

and then remove the source files

Make it unique in the process. But this require sorting

find "$HOME" -name '*.txt' -type f -print0 | sort -zu | xargs -0 cp -t "$HOME/newdir" 

Or if it’s not about the generated files, try to use the -u option of cp .

find "$HOME" -name '*.txt' -type f -print0 | xargs -0 cp -ut "$HOME/newdir" 
-u copy only when the SOURCE file is newer than the destination file or when the destination file is missing

worked perfectly in a Makefile context with docker — thanks!

copy: @echo '' bash -c 'install -v ./docker/shell .' bash -c 'install -v ./docker/docker-compose.yml .' bash -c 'install -v ./docker/statoshi .' bash -c 'install -v ./docker/gui .' bash -c 'install -v ./docker/$(DOCKERFILE) .' bash -c 'install -v ./docker/$(DOCKERFILE_SLIM) .' bash -c 'install -v ./docker/$(DOCKERFILE_GUI) .' bash -c 'install -v ./docker/$(DOCKERFILE_EXTRACT) .' @echo '' build-shell: copy docker-compose build shell 

Источник

Оцените статью
Adblock
detector