What does read -r do? - linux

String="This is a string of words."
read -r -a Words <<< "$String"
I use the man read command to check, but there is no option at all. I also try to search pattern /-r. It comes up with no result. How can I find what does -r actually do?

read is a shell builtin command and you can find information about it from man bash:
read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
...
-r Backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslash-newline pair may not be used as a line continuation.

Related

Parallel and bbcp

I'm trying to use parallels with the file transfer program bbcp but there seems to be some parsing issue that I'm unable to figure out. I run the following command and get the following output -
Command -
cat testfiles.log | parallel --will-cite -j 5 bbcp -4 c -r -z -Vv -P 2 -S "ssh -x -a -oFallBackToRsh=no %I -l %U %H module load bbcp; bbcp" {} ./testing_parallel/
Output -
bbcp: /source/path is a directory.
bbcp: Network is unreachable unable to connect to port [::ffff:IP]:PortNumber
bbcp: Unable to allocate more than 0 of 4 data streams.
bbcp: Accept timed out on port PortNumber
bbcp: Unable to allocate more than 0 of 4 data streams.
Usage: bbcp [Options] [Inspec] Outspec
Options: [-a [dir]] [-A] [-b [+]bf] [-B bsz] [-c [lvl]] [-C cfn] [-D] [-d path]
[-e] [-E csa] [-f] [-F] [-g] [-h] [-i idfn] [-I slfn] [-k] [-K]
[-L opts[#logurl]] [-l logf] [-m mode] [-n] [-N nio] [-o] [-O] [-p]
[-P sec] [-r] [-R [args]] [-q qos] [-s snum] [-S srcxeq] [-T trgxeq]
[-t sec] [-v] [-V] [-u loc] [-U wsz] [-w [=]wsz] [-x rate] [-y] [-z]
[-Z pnf[:pnl]] [-4 [loc]] [-~] [-# {copy|follow|ignore}] [-$] [-#] [--]
I/Ospec: [user#][host:]file
**bbcp: Value not specified for '-x'.**
If I use the command on its own without parallel, it does work and doesnt give the 'value not specified' error. Any idea how to get past this?
Edit 1 - anyone??
Maybe try changing the order of the commands.
Instead of:
"ssh -x -a -oFallBackToRsh=no %I -l %U %H module load bbcp; bbcp"
try:
"module load bbcp; ssh -x -a -oFallBackToRsh=no %I -l %U %H bbcp"
Some versions of the bbcp are a bit buggy, and difficult to work with. Also I'm not sure if you can run more than one instances of bbcp in parallel (multiple instances of it would try to connect on the same ports; so you may need to specify port range for each instance)
In your case, I would suggest using an alternative like lftp, without the parallel command, but with the lftp's built-in --parallel[=N] option, which achieves the same thing that you are trying to get with GNU parallel.

cat displays multi-line file content in a single line

In Linux Shell script, I am trying to trace each command status (either Success or Failure) in a log file.
Below is my code snippet to log the entries in a log file.
scp $user#$host:$from $to 2>error.txt
command_run_status=$?
if [[ $command_run_status == 0 ]]; then log_message="secure copy on ("${host}") with ("${user}") is successful"; else log_message="error copying files "`cat error.txt`; fi
./logging.sh "$CURRENT_PID" "$log_message" "D"
Log file is created with an entry as below:
DEBUG 2019-06-21 10:05:35,347 BST [pid1234] [autouser]: error copying files usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user#]host1:]file1 [...] [[user#]host2:]file2
However, I am expecting the log entry as below - error.txt file content with new line character.
DEBUG 2019-06-21 10:05:35,347 BST [pid1234] [autouser]: error copying files
usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
[-l limit] [-o ssh_option] [-P port] [-S program]
[[user#]host1:]file1 [...] [[user#]host2:]file2
Content of error.txt is as below:
usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
[-l limit] [-o ssh_option] [-P port] [-S program]
[[user#]host1:]file1 [...] [[user#]host2:]file2
Can someone comment on the cause of displaying multi-line file content are displaying in a single line in the log file?
What changes are required on the command to print the error file content (with newline character) in the log file?
`cat error.txt`
Bash performs the expansion by executing command in a subshell environment and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.
To prevent word splitting, include the command substitution in double quotes.
echo $(printf 'a\nb\nc')
prints
a b c
while
echo "$(printf 'a\nb\nc')"
prints
a
b
c
(Prefer $(command) to old style `command`).

linux built-in read command flags

I am running into an odd situation. I am on RHEL 6.3. I am trying to use the built-in 'read' command to read the lines in the file in an array and to process it.
In my script, I am using
while IFS=" " read -r -a appInfo
do
echo "4 ${appInfo[0]}"
echo "4 ${appInfo[1]}"
echo "4 ${appInfo[2]}"
echo "4 line *{appInfo[#]}"
currentApp="${appInfo[*]}"
.....
done < details.txt
It is working fine, when I execute the script from the command line. The read command shows the below info
read: usage: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
If I do read --version on command line, I get
Commandline version read (AT&T Research) 2006-12-19
But when I try to execute the exact same script from a Bamboo project, the read command generates an error
read: -a: unknown option
Usage: read [-ACprsv] [-d delim] [-u fd] [-t timeout] [-n nchar] [-N nchar]
[var?prompt] [var ...]
Bamboo : version read (AT&T Research) 2006-12-19
So, if the version shows the same, why do the flags show differently?
What could be the problem. How can I solve the issue, so that I can read the line into an array even when running it from a Bamboo plan?
Thanks to #John1024, I was able to fix the problem by changing the #! line to #!/bin/bash from #!/bin/sh ( which was running the default ksh shell ).
The credit of solution goes to #John1024, just putting this as an answer so that it can be marked as a solution to question.

Read command option not available on older OS Linux .. What other command to allow default value

We have over 500 plus linux hosts with variation of OS running ;
The read commands to prompt users with a default input is not working on soem and works on others.
Is there a command that I can use in all these varied OS to allow default input
2.6.32-504.el6.x86_64 = Works
2.6.18-348.6.1.el5 = Does not work
There is NO I option in the older reads.
read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...]
I basically want to give user option to overwrite the default value at prompt but if they don't I want to take the default value
example:
read -e -p "Enter Your Name:" -i "Allesandra" N
Can you all offer an alternative?
Thanks
One underutilized feature in most shells is the ability to provide a default value for a variable if it is unset.
read -e -p "Enter Your Name:" N
: ${N:=default}
However, I would expect if you have read ... -i ... on a version that doesn't support -i you're going to get an error.
IHTH
Your situation is exactly why the POSIX standard exists; stick with it. Depending on the OS, bash may not even be the default shell.
printf 'Enter Your Name, or leave blank for default:\n'
printf '(Allesandra): '
read name && [ -n "$name" ] || name=Allesandra

Executing multiple commands under as another username within a file in BASH shell

I am attempting to execute a sqlplus command within a file. The file should be able to sudo switch into the oracle user and run the commands needed. The oracle user will need to first source a file with the parameters for the database, then be able to call a sql file from sqlplus.
The script I have is
#!/bin/bash
sudo -iu user oracle -c `
cd /tmp;
. .sourceme;
sqlplus user/password << EOF;
SPOOL logfile.lst;
#runme.sql;
spool off;
exit;
EOF;`
Whether I run the scrupt with sudo or not, it won't give an error. The only thing it will return is the usage for sudo
usage: sudo -h | -K | -k | -L | -V
usage: sudo -v [-AknS] [-p prompt]
usage: sudo -l[l] [-AknS] [-g groupname|#gid] [-p prompt] [-U username] [-u username|#uid] [-g groupname|#gid] [command]
usage: sudo [-AbEHknPS] [-r role] [-t type] [-C fd] [-g groupname|#gid] [-p prompt] [-u username|#uid] [-g groupname|#gid] [VAR=value] [-i|-s] [<command>]
usage: sudo -e [-AknS] [-r role] [-t type] [-C fd] [-g groupname|#gid] [-p prompt] [-u username|#uid] file ...
I figure that this means that my syntax is probably incorrect. However, without putting the command entirely on one line, I'm not sure how to fix the syntax. I've tried using double quotes around the command, removing the semicolon, and changing the grave symbol to use parenthesis instead. None of these produce any results. Trying it as a one liner and removing the parenthesis and grave marks gives me the usage information as well as a bunch of
./background: line 2: spool: command not found
Try using a heredoc:
#!/bin/bash
sudo -iu user oracle <<XXX
cd /tmp;
. .sourceme;
sqlplus user/password << EOF;
SPOOL logfile.lst;
#runme.sql;
spool off;
exit;
EOF;
XXX

Resources