Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -
- Download Here --> https://tinyurl.com/rhf4x3dp (Copy and Paste Link)
- What does `set -x` do?
- @Ole, set is built into bash so if you man bash you can find it in the SHELL BUILTIN COMMANDS section. When viewing the man page you can probably jump to it by searching for this regexp: \bset \[ .
- 3 Answers 3
- set -x enables a mode of the shell where all executed commands are printed to the terminal. In your case it's clearly used for debugging, which is a typical use case for set -x : printing every command as it is executed may help you to visualize the control flow of the script if it is not functioning as expected.
- This is VERY useful! I wish someone had told me that earlier. Are there other interesting tricks like this that can sort of boost up our meta-skill on scripting?
- @Student: set -e at the top of your script will make the script exit with an error whenever an error occurs (and is not explicitly handled). I find that immensely preferable to the default, which is to keep going willy-nilly (like Visual Basic's infamous On Error Resume Next ).
- @JohnZwinck, unfortunately, set -e has very undesirable side effects, to the point that it makes code review almost impossible (because a given line's behavior depends on what's up the call stack -- if a function is called in a "checked" context, set -e is disabled for that invocation and everything it in turn calls). See the exercises in BashFAQ #105.
- @JohnZwinck, Yes, but I have no idea who rking is. Greycat is the author of the BashFAQ, the channel elder of the freenode IRC #bash channel, etc -- if you're going to argue-by-authority, it helps to know who the authorities in a field are. And yes, I absolutely do argue for tediously checking every command's exit handling, just as a code review requires in C or Go, or any other language that doesn't support exceptions. To rely on set -e is to rely on something that is simply not reliable.
- btw: these messages are printed to stderr, so if you for example have such a script running inside a docker container, these messages are interpreted as errors. :)
- set -x
- Prints a trace of simple commands, for commands, case commands, select commands, and arithmetic for commands and their arguments or associated word lists after they are expanded and before they are executed. The value of the PS4 variable is expanded and the resultant value is printed before the command and its expanded arguments.
- Example
- set -x echo `expr 10 + 20 ` + expr 10 + 20 + echo 30 30
- set +x echo `expr 10 + 20 ` 30
- Above example illustrates the usage of set -x . When it is used, above arithmetic expression has been expanded. We could see how a single line has been evaluated step by step.
- To know more about set → visit this link
- when it comes to your shell script,
- Your script might have been printing some additional lines of information when the execution mode selected as DEBUG . Traditionally people used to enable debug mode when a script called with optional argument such as -d
- -u: disabled by default. When activated, an error message is displayed when using an unconfigured variable. -v: inactive by default. After activation, the original content of the information will be displayed (without variable resolution) before the information is output. -x: inactive by default. If activated, the command content will be displayed before the command is run (after variable resolution, there is a ++ symbol).
- Compare the following differences:
- / # set -v && echo $HOME /root / # set +v && echo $HOME set +v && echo $HOME /root / # set -x && echo $HOME + echo /root /root / # set +x && echo $HOME + set +x /root / # set -u && echo $NOSET /bin/sh: NOSET: parameter not set / # set +u && echo $NOSET
- What does "chmod +x " do and how do I use it?
- I want to write the Ubuntu analogue of a "batch file" (a shell script). But I don't know how to use chmod +x filename command to make it so that the script can be run. Nor do I know where to use it.
- As a site note: Linux doesn't use Batch-files. It uses Shell-scripts. These are mostly executed by BASH (Bourne Again SHell).
- @the_Seppi the default shell in Ubuntu is dash (the Debian Almquist Shell) not bash though dash and bash are similar and you can use bash if you want to.
- This may have been unclear, but was never off-topic. Did people think this was about writing actual batch files for Windows and DOS? chmod doesn't really apply to those OSes and "batch file" was in scare quotes. The OP here (last seen 2014) wanted to write and run something similar to a batch file. Unlike in Unix-like OSes, Windows (and DOS) users don't have to use anything like chmod +x to make it so they can launch their scripts the same way they launch executable binaries. I've edited a bit to clarify the question, and I'm voting to reopen.
- @WarrenHill The default shell in Ubuntu, ie. the shell assigned to the newly created user and the shell that starts when that user logs in, is bash. /bin/sh is dash, but it isn't said anywhere that /bin/sh is a "default". You can always start a shell script with #!/bin/bash and not #!/bin/sh (actually that's what I usually do).
- 3 Answers 3
- In Short:
- chmod +x on a file (your script) only means, that you'll make it executable. Right click on your script and chose Properties -> Permissions -> Allow executing file as program, leaves you with the exact same result as the command in terminal.
- If a file you want to change permissions on is located within the systems directory you may need to be root , like so: (be careful, while using sudo command)
- sudo chmod +x /usr/share/testfolder/aFile
- Also it is not clear, what exactly you want to archive here. Please edit your question and give some more detail on the actual problem!
- You can also refer to this question, for more information: chmod u+x' versus 'chmod +x
- In Long:
- Type man chmod in a terminal window ( Ctrl + Alt + T ) and you'll get the following output:
- NAME: chmod - change file mode bits
- chmod [OPTION]. MODE[,MODE]. FILE. chmod [OPTION]. OCTAL-MODE FILE. chmod [OPTION]. --reference=RFILE FILE.
- DESCRIPTION
- This manual page documents the GNU version of chmod. chmod changes the file mode bits of each given file according to mode, which can be either a symbolic representation of changes to make, or an octal number repre‐ senting the bit pattern for the new mode bits. The format of a symbolic mode is [ugoa. ][[+-=][perms. ]. ], where perms is either zero or more letters from the set rwxXst, or a single letter from the set ugo. Multiple symbolic modes can be given, sepa‐ rated by commas. A combination of the letters ugoa controls which users' access to the file will be changed: the user who owns it (u), other users in the file's group (g), other users not in the file's group (o), or all users (a). If none of these are given, the effect is as if a were given, but bits that are set in the umask are not affected. The operator + causes the selected file mode bits to be added to the existing file mode bits of each file; - causes them to be removed; and = causes them to be added and causes unmentioned bits to be removed except that a directory's unmentioned set user and group ID bits are not affected. The letters rwxXst select file mode bits for the affected users: read (r), write (w), execute (or search for directories) (x), execute/search only if the file is a directory or already has execute permission for some user (X), set user or group ID on execution (s), restricted dele‐ tion flag or sticky bit (t). Instead of one or more of these letters, you can specify exactly one of the letters ugo: the permissions granted to the user who owns the file (u), the permissions granted to other users who are members of the file's group (g), and the permissions granted to users that are in neither of the two preceding categories (o). A numeric mode is from one to four octal digits (0-7), derived by adding up the bits with values 4, 2, and 1. Omitted digits are assumed to be leading zeros. The first digit selects the set user ID (4) and set group ID (2) and restricted deletion or sticky (1) attributes. The sec‐ ond digit selects permissions for the user who owns the file: read (4), write (2), and execute (1); the third selects permissions for other users in the file's group, with the same values; and the fourth for other users not in the file's group, with the same values. chmod never changes the permissions of symbolic links; the chmod system call cannot change their permissions. This is not a problem since the permissions of symbolic links are never used. However, for each sym‐ bolic link listed on the command line, chmod changes the permissions of the pointed-to file. In contrast, chmod ignores symbolic links encoun‐ tered during recursive directory traversals.
- SETUID AND SETGID BITS
- chmod clears the set-group-ID bit of a regular file if the file's group ID does not match the user's effective group ID or one of the user's supplementary group IDs, unless the user has appropriate privileges. Additional restrictions may cause the set-user-ID and set-group-ID bits of MODE or RFILE to be ignored. This behavior depends on the policy and functionality of the underlying chmod system call. When in doubt, check the underlying system behavior.
- Change the mode of each FILE to MODE. -c, --changes like verbose but report only when a change is made --no-preserve-root do not treat `/' specially (the default) --preserve-root fail to operate recursively on `/' -f, --silent, --quiet suppress most error messages -v, --verbose output a diagnostic for every file processed --reference=RFILE use RFILE's mode instead of MODE values -R, --recursive change files and directories recursively --help display this help and exit --version output version information and exit Each MODE is of the form `[ugoa]*([-+=]([rwxXst]*|[ugo]))+'.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement