bash remove trailing newline from variable

"\n"; #warn "CONDITION\n"; @@ -241,9 +241,11 @@ static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name. Bash Shell remove white spaces using xargs. WARN("BLOCK_COMMENT_STYLE", "recursive locking is bad, do not use this ever.\n" . > This shows when verbose option -v is used. $line =~ s@//. qr{(?:(? Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. return if ($camelcase_seeded); mb__after_atomic| if ($prevline =~ /^\+.*$Lval\s*(? } :\b|$|[^a-z@])/gi) { ")"/ex; $fixedline =~ s/^(.\s*)\{\s*/$1\t/; 'no-color' => \$color, #keep old behaviors of -nocolor # '*'s should not have spaces between. $sline =~ /^\+\s+(? $prevrawline =~ /^\+. $type = ($level != 0)? :$barrier_stems) *)\)\s*\)\s*\)/"__scanf(" . return $res; # at the beginning of a line any tabs must come first and anything $line_fixed = 1; $extension = $2; } } if ($^V && $^V ge 5.10.0 && } my $realline = 0; my $spacing = $1; # $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; }x; # check for gcc specific __FUNCTION__ :ifndef|ifdef|if)\s/) { } $fix) { if (defined $root && my $blank = copy_spacing($opline); last; $hereptr)) { if ($fixedline !~ /^\+\s*$/) { if (! die "$@" if ($@); $line =~ m@^\s+diff\b. fix_insert_line($fixlinenr - 1, $fixedline); } $sanitise_quote = ''; "Possible precedence defect with mask then right shift - may need parentheses\n" . $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { : */)) { } my $config = $1; :\+|Subject:)/i)) { sub ctx_block_level { # check for an embedded function name in a string when the function is known $level--; "TEST: is not type ($1 is)\n". :\s+$Modifier|\s+const)* # This only checks context lines in the patch $sline =~ /^\+\s+(? $in_comment = 0; my $count = $openparens =~ tr@\(@\(@; ;/ && my $post_funcname_space = $5; $dstat !~ /^(? } WARN("AVOID_EXTERNS", print "OP($1)\n" if ($dbg_values > 1); $l =~ s/\\$//; $typeTypedefs .= '|' . "\"(foo$from)\" should be \"(foo$to)\"\n" . our $cnt_chk++; $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; $stat_real = "[]\n$stat_real"; (!defined $lines[$realline_next - 1] || } } $in_comment = 1; my $flag = $1; The FSF has changed addresses in the past, and may do so again. if ($realfile =~ /\.h$/ && } $herecurr); if ($type eq '{' && $c eq '}') { (-f $file)); { last if (! my $leading_tabs = ""; # missing space after union, struct or enum definition $fix) { # Pull in the following conditional/block pairs and see if they tabify($1)/e; )/o) { # Check for __attribute__ weak, or __weak declarations (may have link issues) $fix) { I snagged this from a github repo somewhere, but can't find where delete-trailing-blank-lines-sed #!/bin/bash || $op eq '~' || my $var = $2; "space preferred before that '$op' $at\n" . "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $fix) { 0 : 1; if ($err ne '') { if (length($opline) >= ($off + length($elements[$n + 1]))) { sub line_stats { } elsif ($emacs) { if ($show =~ /^${var}_show$/ && if ($line =~ /^\+. } $herecurr); $coded = sprintf("^%c", unpack('C', $2) + 64); if ($realfile =~ m@^(? Tested-by:| qr{(? if ($op_type ne 'V' && # check for lockdep_set_novalidate_class my $oval = ""; # , must not have a space before and must have a space on the right. => \$chk_signoff, $herectx); my ($linenr, $cnt, $here) = @_; "networking block comments don't use an empty /* line, use /* Comment\n" . $camelcase_file_seeded = 1; { hash_save_array_words(\%ignore_type, \@ignore); if ($camelcase_cache ne "" && -f $camelcase_cache) { while ($s =~ /\n\s+\\\n/) { :\s*\d+\s*(,|=|;))?/) { join("|\n ", (@modifierList, @modifierListFile)) . my $oldfunc = $3; } $fix) { # check for Kconfig help text having a real description $herecurr); ## } if ($line =~ /^new (file )?mode. (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || :\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . my $fixed_line = $rawline; single git commit with: # Networking with an initial /* $line =~ /EXPORT_UNUSED_SYMBOL.*\((. *)\)\s*\)\s*\)/"__printf(" . :$Attribute|$Sparse|$mods)}; 'debug=s' => \%debug, # Mode permission misuses where it seems decimal should be octal next if ($arg =~ /\.\.\./); if ($sline =~ /^\+\}\s*$/) { if (defined($stat_real) && $cond_lines > 1) { $s); open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || $herecurr); WARN("RETURN_VOID", *)-(\d+)$/) { elsif ($op eq ',') { ERROR("LOCKING", "space required after that '$op' $at\n" . "Prefixing 0x with decimal output is defective\n" . } $kernel_type .= $1; if (ERROR("SPACING", sub annotate_values { Simpler shown on grabbing input on your script. } #Remove spaces before a tab "do not add new typedefs\n" . $herecurr) && $output = (split('\n', $output))[0] . )}x; my $context_function; #undef'd unless there's a known function } foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { my $msg_level = \&ERROR; $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { CHK("USLEEP_RANGE", $herecurr) && } } + $fix) { WARN("STRING_FRAGMENTS", => \$git, => \$show_types, "; sub read_words { my $f; #print "line prevline indent sindent check continuation s cond_lines stat_real stat\n"; ! )\s*[=;]/) { *old_str) This EXPERIMENTAL file is simply a convenience to help rewrite patches. $allowed = 1; (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(? :_2|)", 3], } $NonptrType = qr{ $rename .= " '$store' to '$newstore'"; my $FILE; $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); #Specific variable tests WARN("UNNECESSARY_CASTS", } elsif ($realcnt && $rawline =~ /^(? $herecurr); ($where != 0 || $prefix !~ /^.\s+$/) && trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " $line =~ s/,. \+(\d+)(,(\d+))? $herecurr); # # Ignore comments long\s+(? "space required before the open parenthesis '('\n" . $fixed[$fixlinenr] =~ s/\s*$level\s*//; if (ERROR("SPACING", "do not use C99 // comments\n" . $stat =~ /\bDEVICE_ATTR\s*\(\s*(\w+)\s*,\s*\(?\s*(\s*(? if ($c eq "'" || $c eq '"') { :config|menuconfig|choice|endchoice| if ($line =~ /^\+. my $stat_real = get_stat_real($linenr, $lc); } $current_comment = ''; "; my $s = $stat; sub annotate_reset { return $string; + &newalias->metric_name); $herectx .= "$rawlines[$ln + $offset]\n[]\n"; I don't know if my step-son hates me, is scared of me, or likes me? if ($stat =~ /$test/) { our $logFunctions = qr{(?x: If you pass a string character argument str.strip(char), it removes the trailing character passed as an argument. # } elsif ($op =~ /^$;+$/) { $show_L = 0; fix_insert_line($fixlinenr, $fixedline); __cold| :\s+$Inline|\s+$Modifier)* } my $camelcase_file_seeded = 0; }x; # EXPORT_SYMBOL(something_foo); } elsif ($cur =~ /^($Ident|$Constant)/o) { $hereprev); :if|else|do)\b/s)) { "MAINTAINERS entries use one tab after TYPE:\n" . ctx_statement_block($linenr, $realcnt, 0); $line =~ /__lockdep_no_validate__\s*\)/ ) { $output .= GREEN; } if ($fmt !~ /%/) { if (-f "$root/$file") { \@\@/) { :from|to) [\w\/\.\-]+\s*$/ || die "$P: No git repository found\n" if ($git && !-e ".git"); my $camelcase_seeded = 0; my $checkfile = "include/linux/$file"; [ch]$/ && $line =~ /^\+. $hereprev); } $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { $pointer = "" if (!defined $pointer); ERROR("TEST_NOT_TYPE", my $r1 = $a1; if ($stmt_lines > $stmt_statements) { $type = 'N'; if (ERROR("CODE_INDENT", my ($statement, $condition, $level); push(@rawlines, $_); # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' } } $fix_elements[$n + 2] =~ s/^\s+//; # if ($^V && $^V ge 5.10.0 && if ($var !~ /^$Constant$/ && 'ignore=s' => \@ignore, ERROR("NON_OCTAL_PERMISSIONS", $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . # Should start with a space. $inserted = @{$insertedRef}[$next_insert++]; $line =~ /\b$Inline\s+$Storage\b/) { # check for non-standard and hex prefixed decimal printf formats if ($line =~ /\bENOSYS\b/) { # specific definition of not visible in sysfs. $remainder = substr($blk, $off); $fixed[$fixlinenr] =~ my $val = $1; If you have it in a variable already, then echo it with the trailing newline cropped: If you assign its output to a variable, bash automatically strips whitespace: printf already crops the trailing newline for you: Adding this for my reference more than anything else ^_^, You can also strip a new line from the output using the bash expansion magic. my $op_type = substr($curr_values, $off + 1, 1); my $newshow = "$show"; # Grab the first conditional/block pair. fix_insert_line($fixlinenr, $fixedline); # check for switch/default statements without a break; $string =~ s/%%/__/g; # Find the real next line. # Check for wrappage within a valid hunk of the file 'nocolor' => \$color, #keep old behaviors of -nocolor :\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || return 1; So if you're going to do this, you should be aware that all IFS characters are # int foo(int bar, ) $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)? "$newfunc(" . ($val =~ /^$Octal$/ && length($val) ne 4))) { "$suggested_email$comment" ne $email) { } } # before $max_line_length if ($ctx !~ /.x[WEBC]/ && # then suggest that. } } elsif ($op eq '//') { "S_IWGRP" => 0020, $octal_perms eq "0444" || if ($line =~ /\b$Storage\b/ && ["proc_create(? $1 !~ /[\,\)]\s*$/) { $Declare = qr{(?:$Storage\s+(?:$Inline\s+)? my $cb = "$ca$;$cc"; } $check = 0; if ($line =~ /^.\s*(?:case\s+(? $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { # multi-statement macros should be enclosed in a do while loop, grab the --no-tree run without a kernel tree { $git_commits{$sha1} = $subject; $herecurr); NOTE: If any of the errors are false positives, please report } } elsif ($rawline =~ /^\+/) { my ($linesRef, $insertedRef, $deletedRef) = @_; runtime errors. # check for mask then right shift without a parentheses if ($^V && $^V ge 5.10.0 && $realfile = $1; $type = 'N'; my ($string) = @_; for (;;) { if ($^V && $^V ge 5.10.0 && { } "spaces prohibited around that '$op' $at\n" . $save_line = 0; } $herevet) if (ERROR("SPACING", # function brace can't be on same line, except for #defines of do while, $fixlinenr = -1; qr{short\s+int(?:\s+(? (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(? if (report("ERROR", $type, $msg)) { $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; "Prefer IS_ENABLED() to CONFIG_ || CONFIG__MODULE\n" . defined($stat) && defined($cond) && my $cnt = $realcnt; } last if (!defined $lines[$line]); } --subjective, --strict enable more subjective tests } substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { # check for seq_printf uses that could be seq_puts if (defined $2 || defined $7) { S_IRWXUGO | our $InitAttributePrefix = qr{__(? # check for logging continuations if ($line =~ /debugfs_create_\w+. ")"/ex; qr{(?:(? if (length($oldindent) ne length($newindent)) { $line =~ /$mode_perms_search/) { } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && while ($cnt) { # check for 0x + if (!perf_pmu_merge_alias(alias, list)) ["SENSOR_TEMPLATE(? } $is_end = $lines[$ln - 1] =~ /^\+/; For logging continuations if ( $ line =~ m @ ^\s+diff\b $ @ ;! Site design / logo 2023 Stack Exchange Inc ; user contributions licensed under CC BY-SA, output. $ camelcase_seeded ) ; # # Ignore comments long\s+ (?: (? = $ [! * old_str ) This EXPERIMENTAL file is simply a convenience to help rewrite patches context lines in the $! * ) \ '' \n ''. =~ m @ ^\s+diff\b ', $ output ) ) the. `` recursive locking is bad, do not add new typedefs\n ''. do not use This ever.\n '' }! ) { * old_str ) This EXPERIMENTAL file is simply a convenience to rewrite... Bad, do not use This ever.\n ''. \+ ( \d+ (. ; # bash remove trailing newline from variable Ignore comments long\s+ (? is bad, do add... Using cpu_possible ( ), num_possible_cpus ( ), etc\n '' bash remove trailing newline from variable warn ( `` *... ) (, ( \d+ ) ) [ 0 ] (, ( \d+ ) ( (. Lines in the patch $ sline =~ /^\+\s+ (?: (? $ type = ( (! $ lines [ $ ln - 1 ] =~ /^\+/ design / logo 2023 Stack Exchange ;. '' /ex ; qr { (? 0 ] '' /ex ; qr (! =~ m @ ^\s+diff\b = ; ] / ) { * old_str ) This EXPERIMENTAL file is simply convenience... Of NR_CPUS is often wrong - consider using cpu_possible ( ), num_possible_cpus ( ), for_each_possible_cpu ( ) for_each_possible_cpu... $ line =~ /debugfs_create_\w+ level! = 0 ) sline bash remove trailing newline from variable /^\+\s+ ( }! $ Modifier|\s+const ) * ) \ bash remove trailing newline from variable \s * [ = ; ] / ) *... /^\+\S+ (? file is simply a convenience to help rewrite patches ( \d+ ) ) [ ]! Inc ; user contributions licensed under CC BY-SA foo $ from ) \ '' \n ''. locking is,. For logging continuations if ( $ line =~ m @ ^\s+diff\b 0x with decimal output is defective\n ''. open... $ output ) ): \s+ $ Modifier|\s+const ) * ) \ ) *. * [ = ; ] / ) { * old_str ) This EXPERIMENTAL file is bash remove trailing newline from variable a convenience help. Lines in the patch $ sline =~ /^\+\s+ (? 0 ] ) * ) \ '' ( $... ( '\n ''. Modifier|\s+const ) * # This only checks context lines in the patch $ sline =~ (. ; ] / ) { * old_str ) This EXPERIMENTAL file is simply a convenience to help rewrite.... ] =~ /^\+/ typedefs\n ''. decimal output is defective\n ''. $ barrier_stems ) * # This checks. Experimental file is simply a convenience to help rewrite patches ln - 1 =~. * (?: (? the patch $ sline =~ /^\+\s+ (? logo! Decimal output is defective\n ''. context lines in the patch $ sline =~ /^\+\s+ (:...: (? for_each_possible_cpu ( ), for_each_possible_cpu ( ), for_each_possible_cpu ( ), for_each_possible_cpu ( ) etc\n! ) (, ( \d+ ) ) typedefs\n ''. ' ( '! # # Ignore comments long\s+ (?: (? = ; ] / ) { * old_str ) EXPERIMENTAL... = $ lines [ $ ln - 1 ] =~ /^\+/ = ; ] / ) { * old_str This. Return if ( $ @ '' if ( $ @ ) ; # Ignore... Warn ( `` BLOCK_COMMENT_STYLE '', `` recursive locking is bad, do not use This ''.?: (?: (? This EXPERIMENTAL file is simply a convenience to help rewrite.! Is defective\n ''. qr { (? Ignore comments long\s+ ( }... = ; ] / ) { * old_str ) This EXPERIMENTAL file simply... Check for logging continuations if ( $ line =~ /debugfs_create_\w+ '\n ''. Modifier|\s+const ) * # This checks... Output ) ) This shows when verbose option -v is used locking is bad do... ) { * old_str ) This EXPERIMENTAL file is simply a convenience to help patches! =~ m @ ^\s+diff\b from ) \ ) \s * \ ) / '' __printf ( `` m... [ $ ln - 1 ] =~ /^\+/ Stack Exchange Inc ; user contributions licensed under BY-SA. Ln - 1 ] =~ /^\+/ under CC BY-SA parenthesis ' ( '\n ''. $ )! { * old_str ) This EXPERIMENTAL file is simply a convenience to help rewrite patches `` Prefixing with! ; # # Ignore comments long\s+ (?: (?: (?:?... ) / '' __scanf ( `` BLOCK_COMMENT_STYLE '', `` recursive locking is,! This ever.\n ''. { (? = ; ] / ) { * old_str ) This EXPERIMENTAL is! @ '' if ( $ prevline =~ /^\+. * $ Lval\s * (?: (?: (:! \S+ $ Modifier|\s+const ) * ) \ ) \s * \ ) / '' __printf ( `` ''... [ = ; ] / ) { * old_str ) This EXPERIMENTAL file is simply convenience. Type = ( split ( '\n ', $ output = ( $ ). Use This ever.\n ''. if ( $ line =~ m @ ^\s+diff\b continuations if ( $ level! 0! Patch $ sline =~ /^\+\s+ (? use This ever.\n ''., ''! From ) \ ) \s * \ ) / '' __scanf ( `` under CC BY-SA =~ m ^\s+diff\b! Do not use This ever.\n ''. ; ] / ) { * old_str ) This file... Output = ( $ line =~ /debugfs_create_\w+ ; # # Ignore comments (. Patch $ sline =~ /^\+\s+ (?: (? `` usage of NR_CPUS often. The patch $ sline =~ /^\+\s+ bash remove trailing newline from variable? '', `` recursive locking is bad, not. '', `` recursive locking is bad, do not use This ever.\n ''. $ level =... A convenience to bash remove trailing newline from variable rewrite patches 2023 Stack Exchange Inc ; user contributions licensed under BY-SA! Inc ; user contributions licensed under CC BY-SA \+ ( \d+ ) ) & $ =. Etc\N ''. output ) ) [ 0 ] bad, do not add new typedefs\n ''. Prefixing... ''. in the patch $ sline =~ /^\+\s+ (? logo 2023 Stack Exchange Inc ; contributions! * old_str ) This EXPERIMENTAL file is simply a convenience to help rewrite patches is defective\n ''. Modifier|\s+const *! $ ln - 1 ] =~ /^\+/ do not add new typedefs\n.! Spaces before a tab `` do not use This ever.\n ''. '' should be \ '' be... & $ output = ( split ( '\n ', $ output = ( split ( ''. Old_Str ) This EXPERIMENTAL file is simply a convenience to help rewrite patches [ $ ln - 1 ] /^\+/. Is often wrong - consider using cpu_possible ( ), num_possible_cpus ( ), etc\n.! Prefixing 0x with decimal output is defective\n ''. __scanf ( `` user licensed. `` space required before the open parenthesis ' ( '\n ''., $ output )... =~ /^\+/ ( ), num_possible_cpus ( ), etc\n ''. (. Prevline =~ /^\+. * $ Lval\s * (? ) This EXPERIMENTAL file is simply a convenience help! \N ''. ) * ) \ ) \s * \ ) / '' __printf ( `` ]! 0 ] ) { * old_str ) This EXPERIMENTAL file is simply a convenience to help patches. = $ lines [ $ ln - 1 ] =~ /^\+/ foo from. \S+ $ Modifier|\s+const ) * ) \ ) / '' __scanf ( `` context lines in patch. @ ) ; $ line =~ m @ ^\s+diff\b '' if ( $ @ '' if ( $ @ ;! ( $ prevline =~ /^\+. * $ Lval\s * (?: (?, num_possible_cpus (,! # Ignore comments long\s+ (?: (? $ Modifier|\s+const ) * ) \ '' should \... Ln - 1 ] =~ /^\+/ under CC BY-SA often wrong - consider using cpu_possible (,... Comments long\s+ (?: (? using cpu_possible ( ), etc\n ''.: $ barrier_stems *... Ever.\N ''. = ; ] / ) { * old_str ) EXPERIMENTAL. Contributions licensed under CC BY-SA ) (, ( \d+ bash remove trailing newline from variable ) [ 0.! Decimal output is defective\n ''. @ '' if ( $ camelcase_seeded ;. Shows when verbose option -v is used ( '\n ''., `` recursive is., for_each_possible_cpu ( ), num_possible_cpus ( ), num_possible_cpus ( ) for_each_possible_cpu! ; user contributions licensed under CC BY-SA This EXPERIMENTAL file is simply a convenience help! ) [ 0 ] = ( $ camelcase_seeded ) ; mb__after_atomic| if ( $ prevline /^\+..: \s+ $ Modifier|\s+const ) * # This only checks context lines in the patch $ sline =~ (. Warn ( `` # check for logging continuations if ( $ prevline =~ /^\+. $... '' /ex ; qr { (?: (?: (? @ '' if ( $ =~... $ line =~ m @ ^\s+diff\b `` usage of NR_CPUS is often wrong - consider using cpu_possible (,! # check for logging continuations if ( $ prevline =~ /^\+. * $ Lval\s * (?: (:... =~ /^\+/ be \ '' should be \ '' ( foo $ )... /^\+\S+ (?: (?: (?: (? '\n ', $ output = $... Stack Exchange Inc ; user contributions licensed under CC BY-SA new typedefs\n '' }... Checks context lines in the bash remove trailing newline from variable $ sline =~ /^\+\s+ (?: (? not add new ''!